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
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
 
题目分析:   给定一些特征码,一个目标串,然后要你求包含的特征码的值,值得注意的是,对于目标串需要正反两次扫一遍,然后就是解析目标串,将[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 ;
}

hdu ----3695 Computer Virus on Planet Pandora (ac自动机)的更多相关文章

  1. hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)

    题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...

  2. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  3. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora

    Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...

  5. HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)

    题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...

  6. HDU3695 - Computer Virus on Planet Pandora(AC自动机)

    题目大意 给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以 题解 建立好自动机后.把文本串T正反各匹配一次,刚开始一直TLE...后面找到原因是重复的子串很多以及有模式 ...

  7. POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)

    题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...

  8. hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1

    F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format ...

  9. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora

      Computer Virus on Planet Pandora Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1353 ...

随机推荐

  1. mac 升级vim

    首先,要下载vim的源代码.Vim source archives : vim online,下载7.4的新建一个目录用于安装vim 7.4:sudo mkdir /usr/local进入源代码的sr ...

  2. startup.c

    在Startup.s文件中包含一个startup的入口函数,该函数为EBOOT的最开始的入口.在系统上电或者冷启动的时候,这是第一个被执行的函数.该函数都是由汇编语言编写的,完成基于硬件平台的最初的初 ...

  3. Postgres Plus Advanced Server installation

    # setenforce Permissive # ./ppasmeta-9.3.1.3-linux-x64.run --mode text Installation Directory [/opt/ ...

  4. Webstorm的序列号和证书

    User Name: ------------name-------------- EMBRACE -------------name-------------- License Key: ===== ...

  5. bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群

    网络流,对于每一个行星,将行星所在行到行星连一条流量为1的边,将行星到其所在列连一条流量为1的边,从源点到所有行连一条流量为1的边,将所有列到汇点都连一条流量为1的边,最大流即为答案. 代码 #inc ...

  6. js实现继承的五种方式

    function Parent(firstname) { this.fname=firstname; ; this.sayAge=function() { console.log(this.age); ...

  7. 10. 星际争霸之php设计模式--原型模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  8. 基于eBox的LTC1446驱动

    LTC1446 是linear出品的双通道12bit轨对轨DAC芯片,采用SPI接口,内部基准电压,满量程输出4.095v,单电源供电(4.5-5v).8Pin封装.            使用时非常 ...

  9. Bison

  10. Runnable和Thread的区别 (转)

    在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的 run ...