标签:title max des 节点 miss time print memset cep
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 41046 Accepted Submission(s):
14830
1 #include<cstdio> 2 #include<cstring> 3 const int MAXN = 1000100; 4 5 struct Trie 6 { 7 int ch[MAXN][26]; 8 int val[MAXN]; 9 int size;//节点总数 10 void Clear() 11 { 12 size = 1; 13 memset(ch[0],0,sizeof(ch[0])); 14 memset(val,0,sizeof(val)); 15 } 16 int idx(char c) 17 { 18 return c - ‘a‘; //字符c的编号 19 } 20 void Insert(const char*s) 21 { 22 int u = 0, len = strlen(s); 23 for (int i=0; i<len; ++i) 24 { 25 int c = idx(s[i]); 26 if (!ch[u][c]) 27 { 28 memset(ch[size],0,sizeof(ch[size])); 29 ch[u][c] = size++; 30 } 31 u = ch[u][c]; 32 val[u]++; 33 } 34 } 35 int Find(const char* s) 36 { 37 int u = 0 , len = strlen(s); 38 for (int i=0; i<len-1; ++i) 39 { 40 int c = idx(s[i]); 41 if (!ch[u][c]) return 0 ; 42 u = ch[u][c]; 43 } 44 int c = idx(s[len-1]); 45 return val[ch[u][c]] ; 46 } 47 }t; 48 49 int main() 50 { 51 char word[15]; 52 t.Clear(); 53 while (gets(word)&&strcmp(word,"")) 54 { 55 t.Insert(word); 56 } 57 while (scanf("%s",word)!=EOF) 58 printf("%d\n",t.Find(word)); 59 return 0; 60 }
标签:title max des 节点 miss time print memset cep
原文地址:http://www.cnblogs.com/mjtcn/p/7123288.html