标签:
1 #include<cstdio> 2 #include<malloc.h> 3 #include<cstring> 4 using namespace std; 5 6 const int maxn=26; 7 8 struct Tire 9 { 10 Tire *next[maxn]; 11 int v; 12 Tire() 13 { 14 memset(next,0,sizeof(next)); 15 v=0; 16 } 17 }; 18 19 Tire *root=new Tire; 20 21 void creatTire(char *str) 22 { 23 int len=strlen(str); 24 Tire *p=root; 25 for(int i=0;i<len;i++) 26 { 27 int id=str[i]-‘a‘; 28 if(p->next[id]==NULL) 29 { 30 Tire *q=new Tire; 31 q->v=1; 32 p->next[id]=q; 33 p=p->next[id]; 34 } 35 else 36 { 37 p->next[id]->v++; 38 p=p->next[id]; 39 } 40 } 41 } 42 43 int findTire(char *str) 44 { 45 int i; 46 int len=strlen(str); 47 Tire *p=root; 48 for(i=0;i<len;i++) 49 { 50 int id=str[i]-‘a‘; 51 if(p->next[id]==NULL) 52 return 0; 53 p=p->next[id]; 54 } 55 return p->v; 56 } 57 58 int main() 59 { 60 //freopen("in.txt","r",stdin); 61 char s[11]; 62 while(gets(s)&&s[0]!=‘\0‘) 63 { 64 creatTire(s); 65 } 66 while(scanf("%s",s)!=EOF) 67 { 68 int ans=findTire(s); 69 printf("%d\n",ans); 70 } 71 return 0; 72 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4696909.html