Computer Virus on Planet Pandora

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

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
 
Recommend
chenyongfu   |   We have carefully selected several similar problems for you:  3699 3692 3691 3698 3697 

 

  AC自动机,入门题

  在hdu上过了,但是poj没过。hdu要求2000MS,poj竟然要求1000MS,太禽兽了 = =。
  另外附上一张ac自动机,trie树,trie图,后缀树,后缀树组等之间关系的图示:

  题意

  给你T组测试数据,每组测试数据有n个模式串,后面跟着一个母串,你需要输出母串包含模式串的个数。
  母串有的需要展开。

  思路

  根据输入的模式串构造trie图(反转串也算),然后根据trie图进行匹配,用isv[]记录匹配到的模式串,最后统计一共匹配到多少模式串。

  注意

  如果母串包含一个模式串的反转也算。

  代码

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std; #define MAXN 260
#define MAXS 5100010 char ss[MAXS],s[MAXS]; //母串和翻译后的母串 struct Node{
Node* next[];
Node* fail; //失败指针
bool isv; //当前这个串走过了没有
int id; //这个串的编号 Node()
{
memset(next,NULL,sizeof(next));
fail = NULL;
isv = false;
id = ;
} ~Node() //析构函数
{
int i;
for(i=;i<;i++)
if(next[i])
delete(next[i]);
}
}; void Insert(Node* p,char t[],int id) //将t插入到Trie树中
{
int i;
for(i=;t[i];i++){
int tt = t[i] - 'A';
if(!p->next[tt])
p->next[tt] = new Node;
p = p->next[tt];
}
p->id = id;
} void setFail(Node* root) //构建失败指针
{
queue <Node*> q;
Node* cur;
cur = root;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
int i;
for(i=;i<;i++){
if(!cur->next[i]) //当前方向的节点是空节点
continue;
if(cur==root) //当前节点是root,他的下一个节点的fail指针全部指向root
cur->next[i]->fail = root;
Node* t = cur->fail;
while(t!=NULL && !t->next[i]) //找到下一个节点存在或者t走到了根节点的fail指针处NULL
t = t->fail;
if(t)
cur->next[i]->fail = t->next[i];
else
cur->next[i]->fail = root; q.push(cur->next[i]);
}
}
root->fail = root;
} bool isv[MAXN];
void Index(Node* root,char s[]) //母串利用ac自动机进行匹配,将匹配成功的模式串编号标记到isv中
{
int i;
Node* p = root;
for(i=;s[i];i++){
int t = s[i]-'A';
while(p!=root && !p->next[t]) //找到根节点或者找到对应节点
p = p->fail;
if(p->next[t])
p = p->next[t];
Node* q = p;
//每走过一个点就把这个点对应的所有失败节点走一遍,标记已经走过
while(q!=root && !q->isv){
q->isv = true;
if(q->id>)
isv[q->id] = true;
q = q->fail;
}
}
} void Trans(char ss[],char s[]) //将ss展开翻译成s
{
int i;
int j=,num=;
for(i=;ss[i];i++){
if( ('a'<=ss[i] && ss[i]<='z')
|| ('A'<=ss[i] && ss[i]<='Z')) //如果是字母
s[j++] = ss[i];
else if( ''<=ss[i] && ss[i]<='') //如果是数字,计数
num = num* + int(ss[i]-'');
else if( ss[i]==']' ) //如果是']',将']'前的字符复制num-1遍
while(--num)
s[j++] = ss[i-];
}
s[j] = '\0';
} int getAns(int n) //利用isv获得最终结果
{
int i,sum=;
for(i=;i<=n;i++)
sum += isv[i];
return sum;
} int main()
{
int T,n,i;
scanf("%d",&T);
while(T--){
Node* root = new Node;
scanf("%d",&n);
//输入n个模式串
for(i=;i<=n;i++){
char t[];
scanf("%s",t);
Insert(root,t,i);
reverse(t,t + strlen(t)); //翻转
Insert(root,t,i);
}
//构建失败指针
setFail(root);
//输入母串
scanf("%s",ss);
Trans(ss,s); //展开
//匹配,获得结果
memset(isv,,sizeof(isv));
Index(root,s); //用ac自动机开始匹配母串
printf("%d\n",getAns(n)); //根据匹配数据获得结果
delete root;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)的更多相关文章

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

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

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

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

  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. BZOJ 3111: [Zjoi2013]蚂蚁寻路

    Sol DP. 首先观察转折,画画图,看看移动路线,可以非常轻易的发现如果走到起点的下方是回不去的.. 然后它就相当于一个底部是平的,顶部凹凹凸凸的形状,每右转两次或左转两次就会形成小矩阵,这样就可以 ...

  2. 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别

    表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...

  3. rpm包制作(待实验)

    作者:firefoxbug 时间:July 18, 2014 rpm包命名规范 对于rpm包的命名符合如下规范. %{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}.rpm N ...

  4. linux上进程状态查询

    linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有 ...

  5. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  6. POJ 1661

    http://poj.org/problem?id=1661 这是一道DP的题目,求最优解 上面的这一个题是对于那个重左边开始上的函数的解释 题目要求的是从最高掉下来的小时间,那么我们就可以求从最低处 ...

  7. ls常用选项总结

    参考: http://billie66.github.io/TLCL/book/index.html ls - List directory contents 选项 长选项 描述 -a --all 列 ...

  8. Vue 为什么在 HTML 中监听事件?

    为什么在 HTML 中监听事件? 你可能注意到这种事件监听的方式违背了关注点分离(separation of concern)传统理念.不必担心,因为所有的 Vue.js 事件处理方法和表达式都严格绑 ...

  9. js中apply方法的使用

    js中apply方法的使用   1.对象的继承,一般的做法是复制:Object.extend prototype.js的实现方式是: Object.extend = function(destinat ...

  10. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...