标签:include while eof code ons .com https href ret
题目连接:https://hihocoder.com/problemset/problem/1014
1 #include<cstdio> 2 #include<cstring> 3 const int maxn=110; 4 struct trie 5 { 6 char a; 7 int cnt; 8 trie* nex[26]; 9 trie() 10 { 11 cnt=0; 12 memset(nex,NULL,sizeof(nex)); 13 } 14 void init() 15 { 16 cnt=0; 17 memset(nex,NULL,sizeof(nex)); 18 } 19 }; 20 trie rt; 21 int ans; 22 23 inline int id(char c) 24 { 25 return c-‘a‘; 26 } 27 28 void inser(char *s) 29 { 30 int len=strlen(s); 31 trie* head=&rt; 32 for(int i=0;i<len;i++) 33 { 34 if(head->nex[id(s[i])]==NULL) 35 { 36 trie *node=new trie; 37 node->a=s[i]; 38 head->nex[id(s[i])]=node; 39 } 40 head=head->nex[id(s[i])]; 41 head->cnt++; 42 } 43 } 44 void fin(char *s) 45 { 46 int len=strlen(s); 47 trie *head=&rt; 48 ans=0; 49 for(int i=0;i<len;i++) 50 { 51 if(head->nex[id(s[i])]==NULL) return; 52 head=head->nex[id(s[i])]; 53 } 54 ans=head->cnt; 55 } 56 int main() 57 { 58 char s[maxn]; 59 int n,m; 60 while(scanf("%d",&n)!=EOF) 61 { 62 rt.init(); 63 for(int i=0;i<n;i++) 64 { 65 scanf("%s",s); 66 inser(s); 67 } 68 scanf("%d",&m); 69 while(m--) 70 { 71 scanf("%s",s); 72 fin(s); 73 printf("%d\n",ans); 74 } 75 } 76 }
标签:include while eof code ons .com https href ret
原文地址:http://www.cnblogs.com/yijiull/p/6741714.html