nyoj 290
動物統計加強版
時間限制:3000?ms ?|? 內存限制:150000?KB 難度:4 描述[陳玉 張云聰]原創
AC代碼:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int data_TrieNode; struct TrieNode{int data;TrieNode *next[26];void init(){data=0;memset(next,0,sizeof(next));} };TrieNode *root=NULL; int Build(char *c){TrieNode *p=root;TrieNode *q=NULL;int i,v,l=strlen(c);for(i=0;i<l;i++){v=c[i]-'a';if(p->next[v]==NULL){q=new TrieNode();p->next[v]=q;p=p->next[v];}else p=p->next[v];}p->data++;return p->data; } int main() {int n,s,i,p;char c[15],str[15];while(cin>>n){s=0;root=new TrieNode(); for(i=0;i<n;i++){scanf("%s",c);p=Build(c);if(s<p){s=p;strcpy(str,c);}}printf("%s %d\n",str,s);}return 0; }WR代碼:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int data_TrieNode; struct TrieNode{int data;TrieNode *next[26];void init(){data=0;memset(next,0,sizeof(next));} }Tree[4000001]; inline TrieNode *new_TrieNode(){ Tree[data_TrieNode].init(); return &Tree[data_TrieNode++]; } TrieNode *root=NULL; void Build(char *c){TrieNode *p=root;TrieNode *q=NULL;int i,v,l=strlen(c);for(i=0;i<l;i++){v=c[i]-'a';if(p->next[v]==NULL){q=new_TrieNode();p->next[v]=q;}p=p->next[v];p->data++;} } int Find(char *c){int i,v,l=strlen(c);TrieNode *p=root;for(i=0;i<l;i++){v=c[i]-'a';if(p->next[v]==NULL){return 0;break;}else p=p->next[v];}return p->data; } int main() {int n,s,i,p;char c[4000001][15];while(cin>>n){data_TrieNode=0;s=0;root=new_TrieNode(); for(i=0;i<n;i++){scanf("%s",c[i]);Build(c[i]);}for(i=0;i<n;i++){p=Find(c[i]);if(s<p){s=p;strcpy(c[0],c[i]);} }printf("%s %d\n",c[0],s);}return 0; }總結
- 上一篇: IDEA如何像Eclipse打开多个项目
- 下一篇: 深搜与广搜