hdu ----3695 Computer Virus on Planet Pandora (ac自动机)

Computer Virus on Planet Pandora

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)
Total Submission(s): 2975    Accepted Submission(s):
833

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
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
 
题目分析:   给定一些特征码,一个目标串,然后要你求包含的特征码的值,值得注意的是,对于目标串需要正反两次扫一遍,然后就是解析目标串,将[23c]解压成正常的字符串形式就可以了。
 
代码:
 
 
 #include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<iostream> using namespace std; const int maxn=;
struct Trie{ Trie *child[maxn];
Trie * fail ;
int tail; //函数不占用内存空间
void init(){
for(int i=;i<maxn ;i++ )
child[i]=NULL;
fail=NULL;
tail=;
} }; char var[] ,stt[];
char stmp[]={'\0'}; //构建一个Trie树
void BuildTrie(char st[] , Trie * root){ Trie * cur ;
for( int i=; st[i] ; i++ ){
int ps=st[i]-'A';
if(root->child[ps]==NULL){
cur = new Trie;
cur->init(); //初始化
root->child[ps]=cur;
}
root = root->child[ps];
} root->tail++;
} //构造失败指正
void BuildFail(Trie * root){
queue<Trie *> sav;
Trie * tmp,*cur ;
sav.push(root);
while(!sav.empty()){
tmp = sav.front();
sav.pop();
for( int i= ; i<maxn ; i++ ){
if(tmp->child[i]!=NULL){
if(tmp==root) {
tmp->child[i]->fail=root;
}
else{
cur =tmp;
while(cur->fail){
if(cur->fail->child[i]!=NULL){
tmp->child[i]->fail = cur->fail->child[i];
break;
}
cur =cur->fail;
} if(cur->fail==NULL)
tmp->child[i]->fail = root ; }
sav.push(tmp->child[i]);
}
}
}
} //查询
int Query( char ss[] , Trie *root){ Trie * tmp=root ,*cur;
int res=,ps=;
for(int i=; ss[i] ;i++){
ps = ss[i]-'A';
while(tmp->child[ps]==NULL&&tmp!=root){
tmp=tmp->fail;
}
tmp = tmp->child[ps];
if(tmp==NULL) tmp=root;
cur=tmp;
while(cur!=root&&cur->tail>){
res+=cur->tail;
cur->tail=;
cur= cur->fail;
}
}
return res ;
} //对字符串进行必要的伸展
int change(char stt[] ,char stmp []){ int i,j;
for(i=,j=; stt[i] ;i++){
if(stt[i]!='['){
stmp[j++]=stt[i];
}else{
int k=,lvar=;
while(stt[++i]>=''&&stt[i]<=''){
lvar=lvar*+(stt[i]-'');
}
for(k=;k<lvar;k++){
stmp[j++]=stt[i];
}
i++;
}
}
stmp[j]='\0';
return j;
} //交换字符
void swap(char * a,char *b){
if(*a!=*b) *a^=*b^=*a^=*b;
}
//对字符串进行反序输入 void Reverse(char * ss,int len){ for(int i=;i<len/;i++){
swap(ss[i],ss[len-i-]);
}
} int main(){ int tes,nm,res;
scanf("%d",&tes);
while(tes--){
res=;
Trie * root = new Trie;
root->init();
scanf("%d",&nm);
while(nm--){
scanf("%s",var);
BuildTrie(var,root);
}
BuildFail(root);
scanf("%s",stt);
int len= change(stt,stmp);
res=Query(stmp,root);
Reverse(stmp,len);
res+=Query(stmp,root);
printf("%d\n",res);
}
return ;
}
上一篇:看看这蛋疼的Java代码


下一篇:ssh免密码登陆设置