标签:
Description
Input
Output
Sample Input
banana band bee absolute acm ba b band abc
Sample Output
2 3 1 0
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 typedef struct Trie 5 { 6 int num; 7 struct Trie *next[26]; 8 bool exist; 9 }node,*trie; 10 node *create() 11 { 12 node *a=(node *)malloc(sizeof(node)); 13 a->num=0; 14 a->exist=false; 15 memset(a->next,0,sizeof(a->next)); 16 return a; 17 } 18 void insert_(trie root,char *s) 19 { 20 trie a=root; 21 char *p=s; 22 int id; 23 while(*p) 24 { 25 id=*p-‘a‘; 26 if(a->next[id]==NULL) 27 { 28 a->next[id]=create(); 29 } 30 a=a->next[id]; 31 p++; 32 a->num+=1; 33 } 34 a->exist=true; 35 } 36 int finds(trie root,char *s) 37 { 38 trie a=root; 39 char *p=s; 40 int id; 41 while(*p) 42 { 43 id=*p-‘a‘; 44 a=a->next[id]; 45 ++p; 46 if(a==NULL) 47 { 48 return 0; 49 } 50 } 51 return a->num; 52 } 53 int main() 54 { 55 trie root=create(); 56 char str[12]; 57 bool judge=false; 58 while(gets(str)) 59 { 60 if(judge) 61 { 62 printf("%d\n",finds(root,str)); 63 } 64 else 65 { 66 if(strlen(str)!=0) 67 insert_(root,str); 68 else 69 judge=true; 70 } 71 } 72 return 0; 73 }
标签:
原文地址:http://www.cnblogs.com/PrayG/p/5899650.html