1、原题解链接
2、(转)浅谈trie树链接
关于输入:用gets输入可以读入空行
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 400001
int nextt[maxn][26];
int sum[maxn];
char s[11];
int root;
int cnt=0;
void insert()
{
int root=0;
for(int i=0;i<strlen(s);i++)
{
int temp=s[i]-'a';
if(!nextt[root][temp]) nextt[root][temp]=++cnt;
sum[nextt[root][temp]]++;
root=nextt[root][temp];
}
}
int search()
{
int root=0;
for(int i=0;i<strlen(s);i++)
{
int temp=s[i]-'a';
if(!nextt[root][temp]) return 0;
else root=nextt[root][temp];
}
return sum[root];
}
int main()
{
int flag=1;
while(gets(s))
{
if(flag==1)
{
if(strlen(s)!=0) insert();
else flag=0;
}
else cout<<search()<<endl;
}
}