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 ...
随机推荐
- 大数据_zookeeper环境搭建中的几个坑
文章目录 [] Zookeeper简介 关于zk的介绍, zk的paxos算法, 网上已经有各位大神在写了, 本文主要写我在搭建过程中的几个极有可能遇到的坑. Zookeeper部署中的坑 坑之一 E ...
- XF 列表视图绑定集合
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- ControlTemplate
ControlTemplate:外观定制 <Window.Resources> <ControlTemplate x:Key="CheckBoxControlTemplat ...
- XF 滑块和步进控件
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http:/ ...
- jquery 调用js成员
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- TVideoCapture类的源码,继承TCustomPanel,用于视频捕获(用到了SendMessage和SetWindowPos等API)good
unit VideoCapture; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, ...
- 使用Cubic Spline通过一组2D点绘制平滑曲线
原文Draw a smooth curve through a set of 2D points with Cubic Spline I would like to provide you with ...
- AY写给国人的教程- VS2017 Live Unit Testing[2/2]-C#人爱学不学-aaronyang技术分享
原文:AY写给国人的教程- VS2017 Live Unit Testing[2/2]-C#人爱学不学-aaronyang技术分享 谢谢大家观看-AY的 VS2017推广系列 Live Unit Te ...
- 命名管道的C#实现
1. 命名管道简介 "命名管道"或"命名管线"(Named Pipes)是一种简单的进程间通信(I P C)机制,Microsoft Windows NT ...
- thinkphp前台html格式化输出日期
输出格式: {$time|strtotime|date="Y年m月d日",###} 格式说明: $time 是日期字符串,一般后台的时间是"Y-m-d H:i:s&quo ...