模板题,节点数量尽可能开大点。输入方式注意一下
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=2e6+5;
int tree[maxn][30];
bool flagg[maxn];
int tot;
char s[maxn];
int a[maxn];
void insert_(char *str)
{
int len=strlen(str);
int root=0;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
if(!tree[root][id]) tree[root][id]=++tot;
root=tree[root][id];
a[root]++;
}
flagg[root]=true;
}
int find_(char *str)//查询操作,按具体要求改动
{
int len=strlen(str);
int root=0;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
if(!tree[root][id]) return 0;
root=tree[root][id];
}
return a[root];
}
int main()
{
while(gets(s)){
if(s[0]=='\0')
break;
insert_(s);
}
while(gets(s)){
printf("%d\n",find_(s));
}
return 0;
}