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. MySQL基础CRUD编程练习题的自我提升(1)

    基础知识: 1.数据库的连接 mysql -u -p -h -u 用户名 -p 密码 -h host主机 2:库级知识 2.1 显示数据库: show databases; 2.2 选择数据库: us ...

  2. Java 画图

    package com.lf.testproxy; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; i ...

  3. lua module package.seeall选项

    module 与 package.seeall http://blog.codingnow.com/2006/02/lua_51_module.html 使用 module("test&qu ...

  4. Comet 反Ajax: jQuery与PHP实现Ajax长轮询

    原文地址(http://justcode.ikeepstudying.com/2016/08/comet-%E5%8F%8Dajax-%E5%9F%BA%E4%BA%8Ejquery%E4%B8%8E ...

  5. FlashFXP5_gr坑爹的故事

    数据中心说已把数据存放到ftp上,但我通过flashfxp5工具链接到ftp server查看数据中心存放的数据,一天了都没有看到数据结果,经过我反复多次重新链接否没有发现数据中心所说的最新数据结果, ...

  6. [原创]java WEB学习笔记94:Hibernate学习之路---session 的管理,Session 对象的生命周期与本地线程绑定

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  8. Git使用方法

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  9. SQL中 Left Join 与 Right Join 与 Inner Join 与 Full Join的区别

    首先看看Left Join 与Right Join 与 Inner Join 与 Full Join对表进行操作后得到的结果. 在数据库中新建两张表,并插入要测试的数据. 新建表: GO /***** ...

  10. Bug测试报告--连连看——天天向上

    测试时间:2016-11-23 20:10 测试者:刘芳芳(nice!团队) 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git. ...