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. [转]IntelliJ Idea 常用快捷键 列表(实战终极总结!!!!)

    IntelliJ Idea 常用快捷键 列表(实战终极总结!!!!) ntelliJ Idea 常用快捷键 列表(实战终极总结!!!!) 1. -----------自动代码-------- 常用的有 ...

  2. Hexo

    Hexo Hexo is a fast, simple & powerful blog framework powered by Node.js.

  3. 【leetcode】Wildcard Matching

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  4. FastReport中文网

    FastReport中文网 http://www.fastreportcn.com/Article/2.html

  5. Application.AddMessageFilter(this);

    开发环境:windows 8(x64), vs2013 只要“项目属性-调试”中选中“启用Visual Studio承载进程“,在VS2013中用F5调试,调用Application.AddMessa ...

  6. 关于js事件委托

    由于事件处理程序可以为现代 Web 应用程序提供交互能力,因此许多开发人员会不分青红皂白地 向页面中添加大量的处理程序. 在创建 GUI 的语言(如 C#)中,为 GUI 中的每个按钮添加一个 onc ...

  7. SQL删除约束

    )禁止所有表约束的SQL select 'alter table '+name+' nocheck constraint all' from sysobjects where type='U' )删除 ...

  8. Android 启动白屏或者黑屏闪现解决

    1.设置Style //1.设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme&qu ...

  9. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  10. 【mongo】drop不释放磁盘空间

    用drop删除mongo的collection后,其size归零,但是storage仍然是原大小,磁盘空间没有被释放. 要用下面命令释放无用的磁盘空间 mongod -repair