hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
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
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.
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.
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’.
AC自动机,入门题

题意
思路
注意
代码
#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自动机,入门题)的更多相关文章
- hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...
- HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)
题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...
- HDU3695 - Computer Virus on Planet Pandora(AC自动机)
题目大意 给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以 题解 建立好自动机后.把文本串T正反各匹配一次,刚开始一直TLE...后面找到原因是重复的子串很多以及有模式 ...
- POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)
题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...
- 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 ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
随机推荐
- 怎样将runlmbench 获取的数值传给上层app
前面那个随笔 , 已经成功将runlmbench 移植到了Android , 并成功的运行. 今天就写一下将runlmbench 获取的那些性能值传给上层 App 进行人机交互. 一开始 , 我是想直 ...
- Github Bash
第一步生成密钥:ssh-keygen -C 'your@email.address' 第二步验证结果:ssh -T git@github.com 第三步克隆:git clone https://git ...
- c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel ...
- struts2 校验数据的有效性 2种方式
Struts2的数据校验: 数据的校验分为客户端校验和服务器端两种: 客户端校验:JS完成的校验.(为了提升用户体验.减少用户的输入错误) 服务器端校验:在后台的校验.(必须的.) 手动编码进行校验: ...
- Appium 服务器参数
# Appium 服务器参数 使用方法: `node . [标志]` ## 服务器标志所有的标志都是可选的,但是有一些标志需要组合在一起才能生效. <expand_table> |标志|默 ...
- Unity3d 查找所选的是否引用过某资源
一.使用方式: 1.选择要被查找的资源,右键->Find Reference 2.把资源拽入Res,点Find 3.输出结果见Console //代码 using UnityEngine; us ...
- Unity3d运行时动态修改材质
void Start () { const string MainTexVariableName = "_MainTex"; var renders = gameObject.Ge ...
- pfsense 企业应用实例
从萌生更换公司网关的想法,到选择.测试.部署陆陆续续用时两个月有余.选择的标准是open and free.这期间不断在查阅一些资料,测试了7.8个各开源防火墙产品.这些产品中大多是基于linux,少 ...
- 基础01 dos命令
常见的dos命令: 盘符: 进入指定的盘下面. 操作文件夹: dir 列出当前控制 ...
- Java for LeetCode 229 Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...