HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description
Input
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
题目大意:给n个模式串,一个长串,问有多少个模式串出现在了长串中(翻转后的模式串也算)。
思路:多模式串匹配,裸的AC自动机题。对于一个模式串可能会是另一个模式串的子串的问题,只要每走一步之后把失配指针都走一下即可。不过据说都走一下会超时,所以要给每个点做一个标记,标记这个点的走过了,不用再走一次了。
PS:用指针的孩纸不回收内存会MLE,亲测……
PS2:题目好像没说一个串不能是另一个串的子串,但是我之前没考虑子串(只考虑了一个串是另一个串的后缀)依然AC了……
代码(HDU 1593MS/POJ 1671MS):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAX = ; struct Node {
Node *go[], *fail;
int src;
bool mark;
Node(int _src) {
src = _src;
fail = ; mark = ;
memset(go, , sizeof(go));
}
~Node() {
for(int i = ; i < ; ++i)
if(go[i]) delete go[i];
}
}; void build(Node *root, char *str, int id) {
Node *p = root;
for(int i = ; str[i]; ++i) {
int index = str[i] - 'A';
if(!p->go[index]) p->go[index] = new Node(-);
p = p->go[index];
}
p->src = id;
} void makeFail(Node *root) {
queue<Node*> que;que.push(root);
while(!que.empty()) {
Node *tmp = que.front(); que.pop();
for(int i = ; i < ; ++i) {
if(!tmp->go[i]) continue;
if(tmp == root) tmp->go[i]->fail = root;
else {
Node *p = tmp->fail;
while(p) {
if(p->go[i]) {
tmp->go[i]->fail = p->go[i];
break;
}
p = p->fail;
}
if(!p) tmp->go[i]->fail = root;
}
que.push(tmp->go[i]);
}
}
root->fail = root;
} bool vis[]; void solve(Node *root, char *str) {
Node *tmp = root;
for(char *now = str; *now; ++now) {
int index = *now - 'A';
while(tmp != root && !tmp->go[index]) tmp = tmp->fail;
if(tmp->go[index]) tmp = tmp->go[index];
Node *q = tmp;
while(q != root && !q->mark) {
q->mark = true;
if(q->src > ) vis[q->src] = true;
q = q->fail;
}
}
} int make_ans(int n) {
int ret = ;
for(int i = ; i <= n; ++i)
ret += vis[i];
return ret;
} void trans(char *ss, char *tt) {
for(int i = ; ss[i]; ++i) {
if(isalpha(ss[i])) *tt++ = ss[i];
else {
++i;
int t = ;
while(isdigit(ss[i])) t = t * + ss[i] - '', ++i;
for(int j = ; j < t; ++j) *tt++ = ss[i];
++i;
}
}
*tt = ;
} char ss[MAXN], s[MAXN];
char tmp[MAX]; int main() {
int T, n;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
Node *root = new Node(-);
for(int i = ; i <= n; ++i) {
scanf("%s", tmp);
build(root, tmp, i);
reverse(tmp, tmp + strlen(tmp));
build(root, tmp, i);
}
makeFail(root);
scanf("%s", ss);
trans(ss, s);
//printf("%s\n", s);
memset(vis, , sizeof(vis));
solve(root, s);
printf("%d\n", make_ans(n));
delete root;
}
}
HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)的更多相关文章
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
- POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)
题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...
- hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- HDU3695 - Computer Virus on Planet Pandora(AC自动机)
题目大意 给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以 题解 建立好自动机后.把文本串T正反各匹配一次,刚开始一直TLE...后面找到原因是重复的子串很多以及有模式 ...
- HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
- HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
Description A new Semester is coming and students are troubling for selecting courses. Students ...
- HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)
Description “Farm Game” is one of the most popular games in online community. In the community each ...
随机推荐
- The C Programming Language (second edition) 实践代码(置于此以作备份)
1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...
- chrome 阻止跨域操作的解决方法 --disable-web-security
做chrome插件时,遇到https页面上请求htttp页面资源时被blocked的问题,初苦寻解决方法未果,最后找到: 给chrome加上 --disable-web-security 参数
- hdu 1241 Oil Deposits(水一发,自我的DFS)
解题思路: 1. 遍历扫描二维数组,遇到‘@’,结果ans++; 2. 将当前 i,j 位置置为‘*’,将当前‘@’的 i,j 传人到DFS函数中,开始遍历八个方向的字符 如果碰到 '@' 则先将当前 ...
- 游标、动态sql、异常
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlIAAAFeCAIAAADBl2bCAAAgAElEQVR4nOyddXgU197H12OEELxIkV
- block与函数指针有什么区别
block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传递参数.同时block本身也可以被作为参数在方法和函数间传递,这就给予了block无限的可能. ...
- Xcode6中添加pch全局引用文件
前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6添加pch文件方法 1. 右键Supporting File,选择“New File” 2. 选择Other ...
- 无法卸载windows组件?提示zClientm.exe
在卸载windows的组件: wmp, outlook, msessager等,提示: 找不到zClientm.exe 文件? 原来, zclientm.exe是windows的游戏组件依赖的文件, ...
- 如何测试本机的公网IP能否被Internet用户访问
声明:本机的公网IP是指ADSL拨号方式取得的公网IP,并非指固定公网IP. 一.新建IIS站点 xp系统需要有安装包才能安装IIS服务 windows7及以上与系统直接在windows->控制 ...
- Android/iOS微信6.3.5同时发布更新 支持群视频聊天、群公告
下午微信6.3.5发布更新,新版最大变化就是支持群视频聊天,又一次向手机QQ靠拢.在群管理方面,支持发布群公告,支持群主转让给其他群成员,同样都是QQ玩剩下的功能.另外,新版支持微信运动查看步数图表. ...
- PHP引用(&)初探:函数的引用返回
函数的引用返回 先看代码: <?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } ...