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): 4090 Accepted Submission(s): 1072
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’.
题目链接:HDU 3695
比较裸的一道题目,拿来熟练默写一下自动机模版还行,主串转换完记得清空数组或者在结尾加上'\0',不然若小于上一次留下来的主串长度则会跟上一次的连在一起造成WA,坑了好久…………
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
const int M=5100010;
struct Aho
{
struct Trie
{
int nxt[26];
int cnt,fail;
inline void reset()
{
CLR(nxt,0);
cnt=fail=0;
}
};
Trie L[260*N];
int tot; void init()
{
L[0].reset();
tot=1;
} void insert(char s[])
{
int len=strlen(s);
int now=0;
for (int i=0; i<len; ++i)
{
int indx=s[i]-'A';
if(!L[now].nxt[indx])
{
L[tot].reset();
L[now].nxt[indx]=tot++;
}
now=L[now].nxt[indx];
}
++L[now].cnt;
} void build()
{
L[0].fail=-1;
queue<int>Q;
Q.push(0);
while (!Q.empty())
{
int now=Q.front();
Q.pop();
for (int i=0; i<26; ++i)
{
if(!L[now].nxt[i])
continue;
if(!now)
L[L[now].nxt[i]].fail=0;
else
{
int v=L[now].fail;
while (~v)
{
if(L[v].nxt[i])
{
L[L[now].nxt[i]].fail=L[v].nxt[i];
break;
}
v=L[v].fail;
}
if(v==-1)
L[L[now].nxt[i]].fail=0;
}
Q.push(L[now].nxt[i]);
}
}
} int cal(int now)
{
int r=0;
while (now)
{
r+=L[now].cnt;
L[now].cnt=0;
now=L[now].fail;
}
return r;
}
int solve(char s[])
{
int len=strlen(s);
int now=0;
int ret=0;
for (int i=0; i<len; ++i)
{
int indx=s[i]-'A';
if(L[now].nxt[indx])
now=L[now].nxt[indx];
else
{
int p=L[now].fail;
while (~p&&!L[p].nxt[indx])
p=L[p].fail;
if(p==-1)
now=0;
else
now=L[p].nxt[indx];
}
if(L[now].cnt)
ret+=cal(now);
}
return ret;
}
};
Aho aho;
char s[N],temp[M],tar[M]; int main(void)
{
int tcase,n,i;
scanf("%d",&tcase);
while (tcase--)
{
CLR(tar,0);//就是这里要清空一下
aho.init();
scanf("%d",&n);
for (i=0; i<n; ++i)
{
scanf("%s",s);
aho.insert(s);
}
aho.build();
scanf("%s",temp);
int len=strlen(temp);
int cnt=0;
for (i=0; i<len; ++i)
{
if(temp[i]>='A'&&temp[i]<='Z')
tar[cnt++]=temp[i];
else
{
++i;
int C=0;
while (temp[i]<'A'||temp[i]>'Z')
{
C=C*10+(temp[i]-'0');
++i;
}
char c=temp[i];
while (C--)
tar[cnt++]=c;
++i;
}
}
int ans=0;
ans+=aho.solve(tar);
reverse(tar,tar+cnt);
ans+=aho.solve(tar);
printf("%d\n",ans);
}
return 0;
}
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 ...
随机推荐
- JS 中面向对象的5种写法
//第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...
- 1140 分珠 dfs
时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不同,珠子之间有一 ...
- Thymeleaf模板引擎使用
Thymeleaf模板引擎使用 什么是Thymeleaf Thymeleaf是一个Java库.它是一个XML / XHTML / HTML5模板引擎,能够在模板文件上应用一组转换,将程序产生的数据或者 ...
- KMP(fail数组应用) LA 3026 Period
题目传送门 题意:(训练指南P213) 求每个前缀的最短循环节 分析:利用失配函数的性质,如果i % (i - fail[i]) == 0,那么正好错位移动一个循环节长度. #include < ...
- BIT LA 4329 Ping pong
题目传送门 题意:训练指南P197 分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了 #include <b ...
- 使用CSS3的appearance属性改变元素的外观
昨天在和同事一起完成项目的时候,我使用了appearance来渲染select,但是在firefox下出现问题,不完美,最后去除了.但还是要学习下这个属性.大家都知道每个浏览器对HTML元素渲染都不一 ...
- ACM: 强化训练-海贼王之伟大航路-dfs-枝减
海贼王之伟大航路 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descriptio ...
- NOIp 2014 #3 寻找道路 Label:图论
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- 【wikioi】1025 选菜
题目链接 算法:01背包DP 此题主要是预处理恶心.我提交了2次...第一次数组开小了...(体积要=V*10) 注意: 钱做为体积,美味价值作为价值 注意,因为体积(钱)是小数点后1位,故数组下标无 ...
- JsonP的简单demo
服务器端代码 public ActionResult GetNewUploadCourseIds() { "; var result = new Result<NewUpload> ...