直接上代码
#include <stdio.h>
#include <string.h>
const int maxn=26;
int idx(char r){
return r-'a';
}
int ch[500000][maxn];
int v[500000];
struct trie{
int sz;
trie(){
sz=1;
memset(ch,0,sizeof(ch));
memset(v,0,sizeof(v));
}
void insert(char *s){
int len=strlen(s);
int u=0;
for(int i=0;i<len;i++)
{
int id=idx(s[i]);
if(!ch[u][id]){
memset(ch[sz],0,sizeof(ch[sz]));
ch[u][id]=sz++;
}
v[ch[u][id]]++;
u=ch[u][id];
}
}
int find(char* s){
int len=strlen(s);
int u=0;
for(int i=0;i<len;i++)
{
int id=idx(s[i]);
if(!ch[u][id]) return 0;
else u=ch[u][id];
}
return v[u];
}
};
int main(){
trie tt;
char t1[100];
while(gets(t1),*t1){
tt.insert(t1);
}
char t2[100];
while(scanf("%s",t2)==1)
{
printf("%d\n",tt.find(t2));
}
return 0;
}