Input 輸入數據的第一部分是一張單詞表,每行一個單詞,單詞的長度不超過10,它們代表的是老師交給Ignatius統計的單詞,一個空行代表單詞表的結束.第二部分是一連串的提問,每行一個提問,每個提問都是一個字符串.
注意:本題只有一組測試數據,處理到文件結束.
Output 對於每個提問,給出以該字符串爲前綴的單詞的數量.
Sample Input
banana band bee absolute acm ba b band abcSample Output
2 3 1 0
#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int M = 1e6+9;
char s[20],s1[20];
int trie[M][26],len,root,tot=0;
ll sum[M];
void inserts()
{
len=strlen(s);
root=0;
for(int i=0;i<len;i++)
{
int pos=s[i]-'a';
if(!trie[root][pos])
trie[root][pos]=++tot;
sum[trie[root][pos]]++;
root=trie[root][pos];
}
}
ll searchs()
{
root=0;
len=strlen(s1);
for(int i=0;i<len;i++)
{
int pos=s1[i]-'a';
if(!trie[root][pos])
return 0;
root=trie[root][pos];
}
return sum[root];
}
int main()
{
while(1)
{
gets(s);
//scanf("%s",s);
len=strlen(s);
//printf("%d\n",len);
if(!len)
break;
inserts();//建樹
}
while(scanf("%s",s1)!=EOF)
{
ll ans=searchs();
printf("%lld\n",ans);
}
return 0;
}