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. 如何在断开ssh连接后仍然保持服务器正常运行程序

    问题描述:当SSH远程连接到服务器上,然后运行一个Python程序(bpr.py),然后把终端开闭(切断SSH连接)之后,发现该程序执行中断. 解决方法:使用nohup命令让程序在关闭窗口(切换SSH ...

  2. C++复现经典游戏——扫雷

    国庆小长假,当大家都去看人山人海的时候,我独自一人狂码代码.这两天想要实现的内容是Windows上的一个经典游戏——扫雷.相信90后和一些上班族对此并不陌生.然而,从win8开始,扫雷就不再是Wind ...

  3. HDU 2571 命运

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  4. paper 104: 彩色图像高速模糊的懒惰算法

    工程及源代码:快速模糊.rar                            图像模糊算法有很多种,我们最常见的就是均值模糊,即取一定半径内的像素值之平均值作为当前点的新的像素值,在一般的工业 ...

  5. 随机采样方法整理与讲解(MCMC、Gibbs Sampling等)

    本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...

  6. oracle表的操作简述

    单表的操作!(代码完全可以用手工代替,写下来为了记忆)1.建立表create table HKB_TABLE_MODIFY(  NAME VARCHAR2(6),  AGE  VARCHAR2(3), ...

  7. grunt让Nodejs规范起来

    Aug 17, 2013 Tags: gruntJavascriptnodejs Comments: 9 Comments grunt让Nodejs规范起来 从零开始nodejs系列文章,将介绍如何利 ...

  8. redis windows下的环境搭建

    先说下安装吧!感觉这东西跟mongodb差不多,安装和布置挺简单,下载地址:https://github.com/dmajkic/redis/downloads 下载下来的包里有两个,一个是32位的, ...

  9. Css Study - 纵向Menu - By html and Css

    http://www.wikihow.com/Create-a-Dropdown-Menu-in-HTML-and-CSS HTML <div id="leftmenu"&g ...

  10. Android--菜单详解

    Android中的菜单分为三种,即选项菜单(系统菜单),上下文菜单和弹出式菜单. 选项菜单: 一个activity只有一个选项菜单,选项菜单的创建方式有低版本创建和高版本创建两种.最常用的是干版本创建 ...