banana band bee absolute acm ba b band abc
2 3 1 0
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #define mem(a, b) memset(a, (b), sizeof(a)) #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Pi(a) printf("%d\n", (a)) #define INF 0x3f3f3f3f #include<algorithm> using namespace std; const int M = 1000000+10; char str[100];//存储单词表 char ask[100];//后来提问的单词 int ch[M][30];//字典树的实现 int vis[M];//标记单词结点 int word[M];//记录当前结点下有多少个单词 int sz;//结点数 void init() { sz = 1; mem(ch[0], 0); mem(word, 0); } int idx(char x) { return x-'a'; } void insert(char *s) { int i, j, len = strlen(s); int u = 0;//从根结点开始 for(i = 0; i < len; i++) { int c = idx(s[i]); if(!ch[u][c])//当前结点不存在 { mem(ch[sz], 0); vis[sz] = 0; ch[u][c] = sz++;//结点数+1 } u = ch[u][c]; word[u]++;//单词数+1 } vis[u] = 1;//标记单词结点 } int find(char *s)//判断字符串s是不是串集里某个字符的前缀 { int i, j, len = strlen(s); int u = 0; for(i = 0; i < len; i++) { int c = idx(s[i]); if(!ch[u][c]) return 0; u = ch[u][c]; } return word[u]; } int main() { init(); while(gets(str), str[0]) { insert(str); } while(scanf("%s", ask)!=EOF) { printf("%d\n", find(ask)); } return 0; }