【HDOJ】2896 病毒侵袭
AC自动机模板题。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define TRIEN 128
#define MAXN 505 typedef struct Trie {
int in;
Trie *fail;
Trie *next[TRIEN];
Trie() {
in = ;
fail = NULL;
memset(next, , sizeof(next));
}
} Trie; Trie *root;
char src[], des[];
bool visit[MAXN]; void create(char str[], int in) {
int i = , id;
Trie *p = root, *q; while (str[i]) {
id = str[i];
++i;
if (p->next[id] == NULL) {
q = new Trie();
p->next[id] = q;
}
p = p->next[id];
}
p->in = in;
} void build_fail() {
int i;
Trie *p, *q;
queue<Trie *> que; for (i=; i<TRIEN; ++i) {
if (root->next[i]) {
root->next[i]->fail = root;
que.push(root->next[i]);
}
} while (!que.empty()) {
p = que.front();
que.pop();
for (i=; i<TRIEN; ++i) {
if (p->next[i]) {
q = p->fail;
while (q != NULL) {
if (q->next[i]) {
p->next[i]->fail = q->next[i];
break;
}
q = q->fail;
}
if (q == NULL)
p->next[i]->fail = root;
que.push(p->next[i]);
}
}
}
} void search() {
int i = , id;
Trie *p = root, *tmp; while (des[i]) {
id = des[i];
++i;
while (p->next[id]==NULL && p!=root)
p = p->fail;
p = p->next[id];
if (p == NULL)
p = root;
tmp = p;
while (tmp != root) {
visit[tmp->in] = true;
tmp = tmp->fail;
}
}
} void del(Trie *t) {
if (t == NULL)
return ;
for (int i=; i<TRIEN; ++i)
del(t->next[i]);
delete t;
} int main() {
int n, m, cnt, total;
int i; while (scanf("%d",&n) != EOF) {
root = new Trie();
for (i=; i<=n; ++i) {
scanf("%s", src);
create(src, i);
}
build_fail();
scanf("%d", &m);
total = ;
for (i=; i<=m; ++i) {
scanf("%s", des);
memset(visit, false, sizeof(visit));
search();
cnt = ;
for (int j=; j<=n; ++j) {
if (visit[j]) {
if (!cnt)
printf("web %d:", i);
++cnt;
printf(" %d", j);
}
}
if (cnt) {
printf("\n");
++total;
}
}
printf("total: %d\n", total);
del(root);
} return ;
}
【HDOJ】2896 病毒侵袭的更多相关文章
- hdoj 2896 病毒侵袭(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另 ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- HDU 2896 病毒侵袭(AC自动机水)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU 2896 病毒侵袭(AC自动机)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2896 病毒侵袭 AC自动机 基础题
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU 2896 病毒侵袭
Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福 ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
随机推荐
- 史上最全WebView使用,附送Html5Activity一份
本文来自:http://www.jianshu.com/users/320f9e8f7fc9/latest_articles感谢您的关注. WebView在现在的项目中使用的频率应该还是非常高的.我个 ...
- Java基础知识强化之集合框架笔记24:ArrayList存储自定义对象并遍历
1. ArrayList存储自定义对象并遍历 2. 代码示例: Student.java,如下: package cn.itcast_01; public class Student { privat ...
- css考核点整理(十一)-响应式开发经验,响应式页面的三种核心技术是什么
响应式开发经验,响应式页面的三种核心技术是什么
- iOS UIKit:viewController之定义(2)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- linux系统下安装wget。
我们先安装linux系统比如centos7.1里面有的就没有wget下载工具.wget这个命令就不可以使用. 我们使用 yum -y install wget yum install perl 会出现 ...
- 解决dispaly:inline-block 遗留间隙的问题
今天做一个项目 .本来我打算是作成表格的 ,后来觉得太费事直接搞成一个div 里面直接放四个a ,然后我将a 设置成inline-block.刚开始还没发现任何间隙问题,(对了说到这里 博主给新手介绍 ...
- ASP.NET和支付宝合作开发第三方接口的注意事项
最近公司和支付宝合作开发第三方接口的项目,这里把过程中需要注意的地方说明一下: 前提:一般来说单个银行不接收个人或私企开通支付接口.因此,和第三方支付公司合作,签订合约开放接口就是通行的做法. 流程: ...
- VS2015升级Update2之后Cordova程序提示:此应用程序无法在此电脑上运行
VS2015在升级到Update2之后,有可能出现如下异常,在运行Cordova项目时提示: 查看输出面板会有乱码错误信息: 出现此问题的原因是在于npm程序损坏了.vs调用的npm程序并不是在nod ...
- .Net framework.
Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...
- RAC配置、安装
RAC 配置及安装 2012年12月30日 星期日 21:49 ******************************************************************* ...