标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 31479 Accepted Submission(s): 12087
1 #include <cstdio> 2 #include <cstring> 3 #include <malloc.h> 4 #include <iostream> 5 using namespace std; 6 #define MAXN 26 7 typedef struct Trie { 8 int v;//根据需要变化 9 Trie *next[MAXN]; 10 //next是表示每层有多少种类的数,如果只是小写字母,则26即可, 11 //若改为大小写字母,则是52,若再加上数字,则是62了 12 } Trie; 13 Trie root; 14 15 void createTrie(char *str) { 16 int len = strlen(str); 17 Trie *p = &root, *q; 18 for(int i = 0; i < len; i++) { 19 int id = str[i]-‘a‘; 20 if(p->next[id] == NULL) { 21 q = (Trie *)malloc(sizeof(root)); 22 q->v = 1;//初始v==1 23 for(int j = 0; j < MAXN; j++) 24 q->next[j] = NULL; 25 p->next[id] = q; 26 p = p->next[id]; 27 } else { 28 p->next[id]->v++; 29 p = p->next[id]; 30 } 31 } 32 // p->v = -1;//若为结尾,则将v改成-1表示 33 } 34 35 int findTrie(char *str) { 36 int len = strlen(str); 37 Trie *p = &root; 38 for(int i = 0; i < len; i++) { 39 int id = str[i]-‘a‘; 40 p = p->next[id]; 41 if(p == NULL) //若为空集,表示不存以此为前缀的串 42 return 0; 43 // if(p->v == -1) //字符集中已有串是此串的前缀 44 // return -1; 45 } 46 return p->v; 47 //return -1; //此串是字符集中某串的前缀 48 } 49 int dealTrie(Trie* T) { 50 //动态字典树,有时会超内存,这是就要记得释放空间了 51 if(T==NULL) 52 return 0; 53 for(int i = 0; i < MAXN; i++) { 54 if(T->next[i]!=NULL) 55 dealTrie(T->next[i]); 56 } 57 free(T); 58 return 0; 59 } 60 int main() { 61 char str[15]; 62 for(int i = 0; i < MAXN; i++) 63 root.next[i] = NULL; 64 while(gets(str) && str[0]!=‘\0‘) 65 createTrie(str); 66 memset(str, 0, sizeof(str)); 67 while(scanf("%s", str) != EOF) { 68 int ans = findTrie(str); 69 printf("%d\n", ans); 70 } 71 return 0; 72 }
set方法:
1 #include <cstdio> 2 #include <iostream> 3 #include <map> 4 #include <cstring> 5 #include <string> 6 using namespace std; 7 8 int main() 9 { 10 char str[17]; 11 map<string, int> m; 12 while(gets(str)) 13 { 14 int len = strlen(str); 15 if (!len) 16 { 17 break; 18 } 19 for(int i = len; i > 0; i--) 20 { 21 str[i] = ‘\0‘; 22 m[str]++; 23 } 24 } 25 while(gets(str)) 26 { 27 cout<<m[str]<<endl; 28 } 29 30 return 0; 31 }
标签:
原文地址:http://www.cnblogs.com/shanyr/p/5676719.html