題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=1004php
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90644 Accepted Submission(s): 34459
node
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 struct node 8 { 9 node *next[26]; 10 int Count; 11 node() 12 { 13 for (int i=0; i<26; i++) 14 next[i]=NULL; 15 Count=0; 16 } 17 }; 18 19 char cc[5000]; 20 node *p,*root=new node(); 21 void insert(char *s) 22 { 23 p=root; 24 for (int i=0; s[i]; i++) 25 { 26 int k=s[i]-'a'; 27 if (p->next[k]==NULL) 28 p->next[k]=new node(); 29 p=p->next[k]; 30 } 31 p->Count++; 32 } 33 34 int Search(char *s) 35 { 36 p=root; 37 for (int i=0; s[i]; i++) 38 { 39 int k=s[i]-'a'; 40 if (p->next[k]==NULL) 41 return 0; 42 p=p->next[k]; 43 } 44 //cout<<p->Count<<" "<<"3333333333"<<endl; 45 return p->Count; 46 } 47 48 int main() 49 { 50 int t; 51 char ch[1010]; 52 int Max; 53 while (~scanf("%d",&t)) 54 { 55 Max=0; 56 if (t==0) 57 break; 58 while (t--) 59 { 60 scanf("%s",ch); 61 //gets(ch); 62 insert(ch); 63 int ans=Search(ch); 64 if (ans>Max) 65 { 66 Max=ans; 67 strcpy(cc,ch); 68 } 69 } 70 printf ("%s\n",cc); 71 } 72 return 0; 73 }