http://acm.hdu.edu.cn/showproblem.php?pid=1251
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 19902 Accepted Submission(s): 8720
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #include <string> 7 #include <map> 8 using namespace std; 9 10 #define MAX 0x7fffffff 11 12 struct node{ 13 node* word[26]; 14 int n; 15 node(){ 16 for(int i=0;i<26;i++) word[i]=NULL; 17 n=1; 18 } 19 }*root; 20 21 void Insert(char* s); 22 int Find(char* s); 23 24 int main(){ 25 //freopen("D:\\input.in","r",stdin); 26 //freopen("D:\\output.out","w",stdout); 27 char tmp[20]; 28 root=new node; 29 while(gets(tmp),strlen(tmp)){ 30 Insert(tmp); 31 } 32 while(gets(tmp)!=NULL){ 33 printf("%d\n",Find(tmp)); 34 } 35 return 0; 36 } 37 void Insert(char* s){ 38 int len=strlen(s); 39 node *current=root,*new_node; 40 for(int i=0;i<len;i++){ 41 if(current->word[s[i]-‘a‘]!=NULL){ 42 current=current->word[s[i]-‘a‘]; 43 current->n++; 44 }else{ 45 new_node=new node; 46 current->word[s[i]-‘a‘]=new_node; 47 current=current->word[s[i]-‘a‘]; 48 } 49 } 50 } 51 int Find(char* s){ 52 int len=strlen(s); 53 node *current=root; 54 for(int i=0;i<len;i++){ 55 if(current->word[s[i]-‘a‘]!=NULL){ 56 current=current->word[s[i]-‘a‘]; 57 }else{ return 0; } 58 } 59 return current->n; 60 }
原文:http://www.cnblogs.com/jiu0821/p/4302033.html