Computer Virus on Planet Pandora
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/128000 K (Java/Others)
Total Submission(s): 1782 Accepted Submission(s): 496
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’.
Source
2010 Asia Fuzhou Regional Contest
Recommend
chenyongfu
本题要求一段程序包含多少个病毒,即对应一个字符串包含多少个模式串,本题是个典型的AC自动机。
首先用病毒对应的模式串建立Tries图,然后还原程序对应的原串,分别沿正向和反向各扫描一遍。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int kind = 26;//视具体情况改动
struct node
{
node *fail; //失败指针
node *next[kind]; //Tire每个节点的26个子节点(最多26个字母)
int count; //是否为该单词的最后一个节点
node()
{ //构造函数初始化
fail=NULL;
count=0;
memset(next,NULL,sizeof(next));
}
}*q[1000*255]; //队列方便用于bfs构造失败指针,大小应依据Tries图//节点个数而定
int head,tail; //队列的头尾指针
node *Root;
//1.建立Tries
void insert(char *str,node *root)
//建立一颗以root为根节点的不带前缀指针的字典树
{
node *p=root;
int i=0,index;
while(str[i])
{
index=str[i]-'A';//视具体情况改动
if(p->next[index]==NULL)
p->next[index]=new node();
p=p->next[index];
i++;
}
p->count=1;
}
//2.建立前缀指针,形成Tries图
void build_ac_automation(node *root)
//在建好的字典树上添加前缀指针,形成Tries图,即ac自动机
{
int i;
root->fail=NULL;
q[head++]=root;
while(head!=tail)
{
node *temp=q[tail++];
node *p=NULL;
for(i=0;i<kind;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root)
temp->next[i]->fail=root;
else
{
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[head++]=temp->next[i];
}
}
}
}
//3.查询母串
int query(node *root,char *s)
//有多少种模式串出现在母串str[]中
{
int i=0,cnt=0,index,len=strlen(s);
node *p=root;
while(s[i])
{
index=s[i]-'A';//视具体情况改动
while(p->next[index]==NULL && p!=root)
p=p->fail;
p=p->next[index];
p=(p==NULL)?root:p;
node *temp=p;
while(temp!=root&&temp->count)
{
cnt+=temp->count;
temp->count=0;
temp=temp->fail;
}
i++;
}
return cnt;
}
//4.初始化
void init()
{
head=tail=0;
Root=new node;
}
char str1[5100000+100],str[5100000+100],str_rev[5100000+100];
//4.还原原串
void change()
{
int i,j,k,len1,num;
len1=strlen(str1);
j=0;
for(i=0;i<len1;i++)
{
if(str1[i]>='A'&&str1[i]<='Z')
{
str[j++]=str1[i];
continue;
}
if(str1[i]=='[')
{
i++;
num=0;
while(str1[i]>='0'&&str1[i]<='9')
{
num=num*10+str1[i]-'0';
i++;
}
for(k=0;k<num;k++)
str[j++]=str1[i];
i++;
}
}
str[j]=0;
for(i=0;i<j;i++)
str_rev[i]=str[j-i-1];
str_rev[i]=0;
// printf("str1=%s str=%s str_rev=%s\n",str1,str,str_rev);
}
int main()
{
int cas,n,i;
cin>>cas;
while(cas--)
{
init();
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",str);
insert(str,Root);
}
build_ac_automation(Root);
scanf("%s",str1);
change();
printf("%d\n",query(Root,str)+query(Root,str_rev));
}
return 0;
}