http://acm.hdu.edu.cn/showproblem.php?pid=2896

题意:中文题意。

思路:AC自动机模板题。主要在于字符有128种,输出还要排序和去重!

注意是“total”不是“totol”!!!因为这个Debug了好久。

还有结点是new的,不然MLE。

主要用来测试模板,看了两个,发现没有注释掉的效率高点。

 #include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define N 100010
#define TOL 128 typedef struct Node {
Node* next[TOL];
Node* fail;
int id;
Node() {
for(int i = ; i < TOL; i++) next[i] = NULL;
fail = NULL; id = ;
}
} node; class AC_DFA { private:
int ans;
node *root; public:
AC_DFA() {
ans = ; root = new Node();
} void insert(char* s, int id) {
node* now = root;
int len = strlen(s);
for(int i = ; i < len; i++) {
char c = s[i];
if(now->next[c] == NULL) now->next[c] = new Node();
now = now->next[c];
}
now->id = id;
} void build() {
root->fail = NULL;
queue<node*> que;
que.push(root);
while(!que.empty()) {
node* now = que.front(); que.pop();
for(int i = ; i < TOL; i++) {
if(now->next[i]) {
node* p = now->fail;
while(p && p->next[i] == NULL) p = p->fail;
if(p) now->next[i]->fail = p->next[i];
else now->next[i]->fail = root;
que.push(now->next[i]);
} else {
if(now == root) now->next[i] = root;
else now->next[i] = now->fail->next[i];
}
}
}
} void match(char *s, int id) {
bool flag = ;
int len = strlen(s);
vector<int> tol;
node* now = root; node* p;
for(int i = ; i < len; i++) {
char c = s[i];
while(now->next[c] == NULL && now != root) now = now->fail;
now = now->next[c];
p = now;
while(p) {
if(p->id) tol.push_back(p->id), flag = ;
p = p->fail;
}
/*
char c = s[i];
now = now->next[c];
p = now;
while(p) {
if(p->id) tol.push_back(p->id), flag = 1;
p = p->fail;
}
*/
} if(!flag) return ;
sort(tol.begin(), tol.end());
int cnt = unique(tol.begin(), tol.end()) - tol.begin(); // 去重
ans++;
printf("web %d: ", id);
for(int i = ; i < cnt; i++) {
printf("%d", tol[i]);
if(i == cnt - ) putchar('\n');
else putchar(' ');
}
} void print() {
printf("total: %d\n", ans); // 不是totol
} }; int main() { AC_DFA ac;
char s[];
int n; scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%s", s); ac.insert(s, i);
}
ac.build();
int m; scanf("%d", &m);
for(int i = ; i <= m; i++) {
scanf("%s", s); ac.match(s, i);
}
ac.print();
return ;
}

HDU 2896:病毒侵袭(AC自动机)的更多相关文章

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

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

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

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

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

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

  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自动机

    题意:略 思路:套用ac自动机模板 #include <iostream> #include<cstdio> #include<cstring> using nam ...

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

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

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

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

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

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

  9. HDU 2896 病毒侵袭【AC自动机】

    <题目链接> Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一 ...

  10. hdu2896 病毒侵袭 ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. 人活着系列Tanya和蔡健雅猪 (floyd)

    人活着系列之芳姐和芳姐的猪 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 芳姐特别喜欢猪,所以,她特意养了m个猪圈,顺便在k条无向边,每条边有都有起点v ...

  2. VMware Workstation克隆linux虚拟机操作

    1.删除MAC地址,修改IP [root@xuegod63 network-scripts]# vim ifcfg-eth0 [root@xuegod63 network-scripts]# cat ...

  3. QWidget继承自QPaintDevice,这样就可以直接把QWidget传入QPainter的构造函数,比如QPainter(mylabel),然后设置QWidget的长宽后直接进行作画了

    比如用QLabel在主界面上画两条虚线: bool ContentWidget::eventFilter(QObject *obj, QEvent *event) { if(obj == line_l ...

  4. ASP Get请求

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. WPF中的资源(二) - 二进制资源

    原文:WPF中的资源(二) - 二进制资源 WPF中的二进制资源,就是类似于MFC中在对话框程序中添加的图片.字符串等资源,程序在运行时将其转换成二进制,以供程序使用. 下面以将字符串转换成二进制为例 ...

  6. Tomcat请求过程

    Tomcat请求过程 1.用户点击网页内容,请求被发送到本机端口8080,被在那里监听的Coyote HTTP/1.1 Connector获得. 2.Connector把该请求交给它所在的Servic ...

  7. android核心系列--2,关于任务栈(task)

    一,任务 任务是由界面组件组成的一个栈,这些界面组件可以来自多个进程,多个应用,为共同完成一项任务而存在,比如写邮件时会用到邮件应用和联系人应用中的界面组件,这些界面组件在同一个任务中运行. 二,界面 ...

  8. Codility---Nesting

    Task description A string S consisting of N characters is called properly nested if: S is empty; S h ...

  9. .NET错误:未找到类型或命名空间名称

    现象:编译项目时提示未找到类型或命名空间名称"... " 解决方法:如果是未找到类型,检查是否引用了类型所在的命名空间,使用using指令:如果是未找到命名空间,那么检查是否引用了 ...

  10. python中的while循环,格式化输出,运算符,编码

    一.while循环 1.1语法 while 条件: 代码块(循环体) else: 当上面的条件为假的的时候,才会执行. 执行顺序:先判断条件是否为真,如果是真的,执行循环体,再次判断条件,直到条件不成 ...