banana band bee absolute acm ba b band abc
2 3 1 0大水题。就不分析了。直接代码:#include <stdio.h> #include <string.h> #include <stdlib.h> struct Trie{ int count; Trie *next[26]; }; void creat(Trie *t , char str[]) { int len = strlen(str) ; for(int i = 0 ; i < len ; ++i) { if(t->next[str[i]-'a'] == NULL) { Trie *temp = (Trie*)malloc(sizeof(Trie)) ; for(int j = 0 ; j < 26 ; ++j) { temp->next[j] = NULL; } temp->count = 0; t->next[str[i]-'a'] = temp ; } t = t->next[str[i]-'a'] ; ++t->count ; } } int count = 0 ; void query(Trie *t , char str[]) { int len = strlen(str) , i ; for(i = 0 ; i < len ; ++i) { if(t->next[str[i]-'a'] == NULL) { break ; } t = t->next[str[i]-'a'] ; } if( i == len) { count += t->count ; } } int main() { int flag = 0; char str[20]; Trie *t = (Trie *)malloc(sizeof(Trie)) ; for(int i = 0 ; i < 26 ; ++i) { t->next[i] = NULL ; t->count = 0 ; } while(gets(str)) { if(strlen(str) == 0) { flag = 1 ; continue ; } if(!flag) { creat(t,str); } else { count = 0 ; query(t,str); printf("%d\n",count) ; } } return 0 ; }
原文地址:http://blog.csdn.net/lionel_d/article/details/43452281