标签:ace title ane ring 单词 panel logs string cep
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 43831 Accepted Submission(s): 15708
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 int tot; 8 char s[11]; 9 struct Trie 10 { 11 int next[27]; 12 int sum; 13 }tr[3000000]; 14 inline void Trie_build() 15 { 16 int now=0,len=strlen(s); 17 for(int x,i=0;i<len;i++) 18 { 19 x=s[i]-‘a‘; 20 if(tr[now].next[x]) 21 now=tr[now].next[x],tr[now].sum++; 22 else 23 { 24 tr[now].next[x]=++tot; 25 now=tot; tr[now].sum++; 26 } 27 } 28 } 29 inline int Trie_find() 30 { 31 int len=strlen(s); 32 int now=0,p=0; 33 for(;p<len;) 34 if(tr[now].next[s[p]-‘a‘]) 35 now=tr[now].next[s[p]-‘a‘],p++; 36 else return 0; 37 return tr[now].sum; 38 } 39 40 int main() 41 { 42 for(;gets(s)&&strlen(s);) 43 { 44 Trie_build(); 45 memset(s,0,sizeof(s)); 46 } 47 for(;gets(s)&&strlen(s);) 48 { 49 printf("%d\n",Trie_find()); 50 memset(s,0,sizeof(s)); 51 } 52 return 0; 53 }
标签:ace title ane ring 单词 panel logs string cep
原文地址:http://www.cnblogs.com/Shy-key/p/7398706.html