标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 20565 Accepted Submission(s): 8949
题解:字典树问题
AC code:
#include <iostream> #include <string> using namespace std; struct T { int num; T* next[26]; T() { num = 0; for(int i=0; i<26; i++) { next[i] = NULL; } } }t; void insert(char s[]) { T* p = &t; for(int i=0; s[i]; i++) { int a = s[i]-‘a‘; if(p->next[a] == NULL) p->next[a] = new T(); p = p->next[a]; p->num++; } } int find(char s[]) { T* p = &t; for(int i=0; s[i]; i++) { int a = s[i]-‘a‘; if(p->next[a]==NULL) return 0; p = p->next[a]; } return p->num; } int main() { char s[11]; while(gets(s)&&strcmp(s,"")!=0) { insert(s); } while(scanf("%s",&s)!=EOF) cout<<find(s)<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/lzeffort/p/4414739.html