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 病毒侵袭的更多相关文章

  1. hdoj 2896 病毒侵袭(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另 ...

  2. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  3. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  5. HDU 2896 病毒侵袭(AC自动机水)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. HDU 2896 病毒侵袭(AC自动机)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. hdu 2896 病毒侵袭 AC自动机 基础题

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. HDU 2896 病毒侵袭

    Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福 ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

随机推荐

  1. IOS-UIProgressView的简单介绍

    IOS-UIProgressView的简单介绍 转载:http://blog.sina.com.cn/s/blog_9c2363ad0101e1jy.html // UIProgressView的使用 ...

  2. Driving the Activity Lifecycle

    Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...

  3. HTML5 ArrayBufferView之DataView

    DataView视图 如果一段数据包括多种类型(比如服务器传来的HTTP数据),这时除了建立ArrayBuffer对象的复合视图以外,还可以通过DataView视图进行操作. DataView视图提供 ...

  4. iOS UICollectionview的详细介绍

    转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...

  5. python中如何使用help命令?

    python下 help()使用方法   查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules ...

  6. 【vc】1_Windows程序内部运行机制

    创建一个Win32应用程序步骤: 1.编写WinMain函数; 2.创建窗口(步骤如下): a.设计(一个)窗口类(WNDCLASS) b.注册(该)窗口类. c.创建窗口. d.显示并更新窗口. 3 ...

  7. 【POJ2886】【线段树】Who Gets the Most Candies?

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  8. 『重构--改善既有代码的设计』读书笔记----Extract Class

    在面向对象中,对于类这个概念我们应该有一个清晰的责任认识,就是每个类应该只有一个变化点,每个类的变化应该只受到单一的因素,即每个类应该只有一个明确的责任.当然了,说时容易做时难,很多人可能都会和我一样 ...

  9. Java学习----this和super(在继承中)

    public class Base { /*public Base() { System.out.println("Base 类的初始构造方法"); }*/ public Base ...

  10. Mvvm绑定datagrid或listview的selectItems的方法[转]

    单选,很简单,将SelectedItem与ViewModel的属性进行双向绑定就OK了 多选,由于ListView的SelectedItems不能进行绑定,需要将ListView的SelectionC ...