字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过.
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
char a[],b[];
struct Node
{
int id,num;
Node *sons[];
Node()
{
id = -,num = ;
for(int j = ; j < ; j++)
sons[j] = NULL;
}
};
Node *root;
Node *now,*Next;
void Build(char *str,int len,int nowid)
{
now = root;
for(int i = ; i < len; i++)
{
int m = str[i] - 'a';
if(now->sons[m] == NULL)
{
Next = new Node;
Next->id = nowid;
Next->num = ;
now->sons[m] = Next;
now = Next;
}
else
{
now = now->sons[m];
if(now->id != nowid)
{
now->id = nowid;
now->num++;
}
}
}
}
int Find(char *str,int len)
{
now = root;
for(int i = ; i < len; i++)
{
int m = str[i] - 'a';
if(now->sons[m] == NULL) return ;
now = now->sons[m];
}
return now->num;
}
void del(Node *root)
{
for(int i = ; i < ; i++)
{
if(root->sons[i] != NULL)
del(root->sons[i]);
}
delete root;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
root = new Node;
for(int i = ; i <= n; i++)
{
scanf("%s",a);
int lena = strlen(a);
for(int j = ; j < lena; j++)
Build(a+j,lena-j,i);
}
scanf("%d",&m);
for(int i = ; i <= m; i++)
{
scanf("%s",b);
int lenb = strlen(b);
printf("%d\n",Find(b,lenb));
}
del(root);
}
return ;
}