Problem Description
Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which they learned from the Earth. On
planet Pandora, hackers make computer virus, so they also have anti-virus software. Of course they learned virus scanning algorithm from the Earth. Every virus has a pattern string which consists of only capital letters. If a virus’s pattern string is a substring of a program, or the pattern string is a substring of the reverse of that program, they can say the program is infected by that virus. Give you a program and a list of virus pattern strings, please write a program to figure out how many viruses the program is infected by.
Input
There are multiple test cases. The first line in the input is an integer T ( T<= 10) indicating the number of test cases.
For each test case:
The first line is a integer n( 0 < n <= 250) indicating the number of virus pattern strings.
Then n lines follows, each represents a virus pattern string. Every pattern string stands for a virus. It’s guaranteed that those n pattern strings are all different so there
are n different viruses. The length of pattern string is no more than 1,000 and a pattern string at least consists of one letter.
The last line of a test case is the program. The program may be described in a compressed format. A compressed program consists of capital letters and
“compressors”. A “compressor” is in the following format:
[qx]
q is a number( 0 < q <= 5,000,000)and x is a capital letter. It means q consecutive letter xs in the original uncompressed program. For example, [6K] means
‘KKKKKK’ in the original program. So, if a compressed program is like:
AB[2D]E[7K]G
It actually is ABDDEKKKKKKKG after decompressed to original format.
The length of the program is at least 1 and at most 5,100,000, no matter in the compressed format or after it is decompressed to original format.
Output
For each test case, print an integer K in a line meaning that the program is infected by K viruses.
Sample Input
3
2
AB
DCB
DACB
3
ABC
CDE
GHI
ABCCDEFIHG
4
ABB
ACDEE
BBB
FEEE
A[2B]CD[4E]F
Sample Output
0
3
2
Hint
In the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected
by virus ‘GHI’.
题目大意
给出n(n<250)个长度小于1000的短字符串,给出一个压缩后的长字符串(展开后长度小于5100000),问长字符串(可翻转)中有多少个短字符串
Solution
AC自动机模板题
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define N 250011
#define L 5100011
int tree[N][26],fail[N],p[N],vis[N],tot,n,t,len,x,ll;
int now,ans,ii,i,j,v,k,l;
char ch[L],s[L];
void insert()
{
now=0;
for (i=1;i<=len;i++)
{
if (tree[now][s[i]-'A']==0) tree[now][s[i]-'A']=++tot;
now=tree[now][s[i]-'A'];
}
p[now]+=1;
}
queue<int> Q;
void get()
{
for (i=0;i<=25;i++)
if (tree[0][i]!=0) Q.push(tree[0][i]);
while (!Q.empty())
{
now=Q.front(); Q.pop();
for (i=0;i<=25;i++)
{
v=tree[now][i];
k=tree[fail[now]][i];
if (v!=0) fail[v]=k,Q.push(v);
else tree[now][i]=k;
}
}
}
void query()
{
ans=0,now=0;
for (i=1;i<=len;i++)
{
now=tree[now][s[i]-'A'];
for (j=now;j!=0;j=fail[j])
{
if (vis[j]==1) break;
ans+=p[j];
p[j]=0;
vis[j]=1;
}
}
now=0;
for (i=len;i>=1;i--)
{
now=tree[now][s[i]-'A'];
for (j=now;j!=0;j=fail[j])
{
if (vis[j]==1) break;
ans+=p[j];
p[j]=0;
vis[j]=1;
}
}
printf("%d\n",ans);
}
void init()
{
memset(tree,0,sizeof(tree));
memset(fail,0,sizeof(fail));
memset(p,0,sizeof(p));
memset(vis,0,sizeof(vis));
tot=0;
scanf("%d",&n);
for (l=1;l<=n;l++)
{
scanf("\n%s",s+1);
len=strlen(s+1);
insert();
}
}
int main()
{
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
init();
get();
scanf("\n%s",ch+1);
len=0;
ll=strlen(ch+1);
for (i=1;i<=ll;i++)
{
if (ch[i]=='[')
{
x=0;
while (ch[i+2]!=']' && i<=ll)
{
i++;
x=x*10+int(ch[i]-'0');
}
for (j=1;j<=x;j++)
s[++len]=ch[i+1];
i+=2;
}
else s[++len]=ch[i];
}
query();
}
return 0;
}
ps
刚写完TLE和MLE和RT轮流轰炸,心态极崩,
然后被ymw(dalao) 发现,tot每次没清零,心塞啊啊啊啊~