[POJ3987 Computer Virus on Planet Pandora]

[关键字]:ac自动机 字符串匹配

[题目大意]:给出一些字符串和一篇文章,问这篇文章和它倒着读的所组成的文章中一共出现过多少个给出的字符串。

//=====================================================================================================

[分析]:其实就是两遍AC自动机匹配,注意一点就是内存问题,POJ卡的很紧我字典树开到600000就超了500000就A了……还有就是我的AC自动机跑的很慢啊954ms差点TLE,反正时间和空间有各种要小心的问题自己注意就行了。

[代码]:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int MAXN=251;
const int MAXL=5100001;

struct node
{
int id,fail;
int next[26];
}ac[500000];
char P1[MAXL],word[MAXN][1001];
int N,test,len,tn,ans;
queue<int> q;

void Insert(char *s)
{
int len=strlen(s);
int p=0;
for (int i=0;i<len;i++)
{
int index=s[i]-'A';
if (!ac[p].next[index]) ac[p].next[index]=++tn;
p=ac[p].next[index];
}
ac[p].id=1;
}

void Build()
{
ac[0].fail=-1;
q.push(0);
while (!q.empty())
{
int u=q.front();
q.pop();
for (int i=0;i<26;i++)
{
int temp=ac[u].next[i],p;
if (temp)
{
for (p=ac[u].fail;p!=-1;p=ac[p].fail)
if (ac[p].next[i]) {ac[temp].fail=ac[p].next[i];break;}
if (p==-1) ac[temp].fail=0;
q.push(temp);
}
}
}
}

void Init()
{
scanf("%d",&N);
for (int i=1;i<=N;i++)
{
scanf("%s",word[i]);
Insert(word[i]);
}
int a;
char ch;
scanf("\n");
for(scanf("%c",&ch);ch!='\n';scanf("%c",&ch))
{
if (ch=='[')
{
scanf("%d",&a);
scanf("%c",&ch);
for (int i=1;i<=a;i++) P1[len++]=ch;
scanf("%c",&ch);
}
else P1[len++]=ch;
}
P1[len]='\0';
Build();
}

void Solve(char *s)
{
int len=strlen(s),p=0;
for (int i=0;i<len;i++)
{
int index=s[i]-'A';
while (!ac[p].next[index]&&p) p=ac[p].fail;
p=ac[p].next[index];
int temp=p;
while (temp&&ac[temp].id!=-1)
{
ans+=ac[temp].id;
ac[temp].id=-1;
temp=ac[temp].fail;
}
}
}

void Prepare()
{
len=tn=ans=0;
memset(ac,0,sizeof(ac));
memset(P1,0,sizeof(P1));
}

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&test);
while (test--)
{
Prepare();
Init();
Solve(P1);
for (int i=0;i<len/2;i++) swap(P1[i],P1[len-1-i]);
//printf("%s\n",P1);
//printf("%d\n",ans);
Solve(P1);
printf("%d\n",ans);
}
return 0;
}



你可能感兴趣的:(com)