HDU 2896:病毒侵袭(AC自动机)
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自动机)的更多相关文章
- 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自动机 基础题
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu 2896 病毒侵袭_ac自动机
题意:略 思路:套用ac自动机模板 #include <iostream> #include<cstdio> #include<cstring> using nam ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
- 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自动机】
<题目链接> Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一 ...
- hdu2896 病毒侵袭 ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新
原文:WPF笔记 ( xmlns引用,Resource.Binding 前/后台加载,重新绑定) 2013.6.7更新 1.xmlns Mapping URI的格式是 clr-namespace:&l ...
- Emgu-WPF 激光雷达研究-移动物体检测
原文:Emgu-WPF 激光雷达研究-移动物体检测 接上篇: https://blog.csdn.net/u013224722/article/details/80738619 先pose出效果图,下 ...
- Delphi 调用C/C++的Dll(stdcall关键字, 会导致函数名分裂. 此时函数名变成_stdadd@8)
delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__std ...
- WPF Layout 系统概述——Arrange
原文:WPF Layout 系统概述--Arrange Arrange过程概述 普通基类属性对Arrange过程的影响 我们知道Measure过程是在确定DesiredSize的大小,以便Arrang ...
- Android 查看Apk签名方式V1和V2
Android 查看Apk签名方式V1和V2 java -jar apksigner.jar verify -v my.apk -- Verifies Verified using v1 scheme ...
- apache本地服务器的配置流程
安装Apache 一.目的: 1. 能够有一个测试的服务器,不是所有的特殊网络服务都能找到免费的! 二.为什么是 "Apache" 1. 使用最广的 Web 服务器 2. Mac自 ...
- 零元学Expression Blend 4 - Chapter 9 用实例了解布局容器系列-「Canvas」
原文:零元学Expression Blend 4 - Chapter 9 用实例了解布局容器系列-「Canvas」 本系列将教大家以实做案例认识Blend 4 的布局容器,此章介绍的布局容器是Blen ...
- Qt 5.6.2 静态编译(VS2013 x86 target xp openssl icu webkit)
在去年4月份的时候,我写过一篇动态编译Qt5.6.0的文章,当时是为了解决webkit不能在winxp下面跑的问题,动态编译有一个缺点,就是发布的时候,要携带一大堆dll,使安装包的体积增大.而静态编 ...
- 你真的懂printf么?
自从你进入程序员的世界,就开始照着书本编写着各种helloworld,大笔一挥: printf("Hello World!\n"); 于是控制台神奇地出现了一行字符串,计算机一句温 ...
- SYN1618型 高精度天文时间同步系统
SYN1618型 高精度天文时间同步系统 产品概述 SYN1618型 高精度天文时间同步系统是由西安同步电子科技有限公司精心设计.自行研发生产的一款高精度的时频频率标准设备,接收GPS.GLON ...