题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896
AC自动机一枚,不解释,分析看前面文章
附上代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
typedefstructpoint{
int count;
struct point *next[94],*fail;
}*Tree,Node;
intdata[3],num;
Tree root;
charstr[10001];
intcmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
Tree NEW()
{
int i;
Tree p;
p=(Tree)malloc(sizeof(Node));
for(i=0;i<94;i++)
p->next[i]=NULL;
p->fail=NULL;
p->count=0;
return p;
}
voidBuild(char ss[],int u)
{
int i=0;
Tree p=root;
while(ss[i])
{
if(p->next[ss[i]-32]==NULL)
p->next[ss[i]-32]=NEW();
p=p->next[ss[i]-32];
i++;
}
p->count=u;
}
voidAC_Automation()
{
int i;
Tree p,temp;
queue<Tree> Q;
Q.push(root);
while(!Q.empty())
{
temp=Q.front();
Q.pop();
p=NULL;
for(i=0;i<94;i++)
{
if(temp->next[i]!=NULL)
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
Q.push(temp->next[i]);
}
}
}
}
intsearch()
{
int k=0,i=0;
Tree p=root,temp;
while(str[i])
{
while(p->next[str[i]-32]==NULL&&p!=root)p=p->fail;
p=p->next[str[i]-32];
if(p==NULL)p=root;
temp=p;