标签:mis author strlen 前缀 main ota names time sel
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 41049 Accepted Submission(s):
14833
#include<cstdio> #include<cstring> using namespace std; #define N 500000 struct node{ bool have; int son[26]; int cnt; int acnt; node(){ have=0; cnt=0; acnt=0; memset(son,0,sizeof(son)); } }trie[N]; int num; void insert(char *s) { int pos=0,len=strlen(s); int fa=0; for(int i=0;i<len;++i) { pos=s[i]-‘a‘; if(!trie[fa].son[pos]){ trie[fa].son[pos]=++num; } fa=trie[fa].son[pos]; trie[fa].cnt++; } trie[fa].have=1; trie[fa].acnt++; } int find(char *s) { int len=strlen (s); int pos,fa=0; for(int i=0;i<len;++i) { pos=s[i]-‘a‘; if(!trie[fa].son[pos]){ return 0; } fa = trie[fa].son[pos]; } return trie[fa].cnt; } int main() { char word[20]; while(gets(word)) { int len=strlen(word); if(len==0)break; else insert(word); } while(gets(word)) { printf("%d\n",find(word)); } return 0; }
标签:mis author strlen 前缀 main ota names time sel
原文地址:http://www.cnblogs.com/sssy/p/7123521.html