HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222
KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法。主要由Trie和KMP的思想构成。
题意:输入N个模式串,再给出一个文本串,求文本串里出现的模式串数目。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 500001
/*
一个模式串的某个字符匹配失败的时候,就跳到它的失败指针上继续匹配,重复上述操作,直到这个字符匹配成功,所以失败指针一定满足一个性质,它指向的一定是某个串的前缀,并且这个前缀是当前结点所在前缀的后缀,而且一定是最长后缀。
*/
struct Ac_DFA
{
int next[N][]; //一开始这里用了char结果MLE
int val[N], size, root, fail[N]; int creat() { //构造新节点
for(int i = ; i < ; i++) {
next[size][i] = -;
}
val[size] = ;
return size++;
} void init() {
size = ;
root = creat();
} void insert(char s[]) { //插入模式串
int len = strlen(s);
int now = root;
for(int i = ; i < len; i++) {
int c = s[i] - 'a';
if(next[now][c] == -) {
next[now][c] = creat();
}
now = next[now][c];
}
val[now]++;
} void build() { //构造fail函数
queue<int> que;
while(!que.empty()) que.pop();
for(int i = ; i < ; i++) { //初始化
if(next[root][i] == -) { //如果没有边就补上去
next[root][i] = root;
} else {
fail[next[root][i]] = root; //有边的话第一个结点指向root
que.push(next[root][i]);
}
}
while(!que.empty()) {
int now = que.front(); que.pop();
for(int i = ; i < ; i++) {
if(next[now][i] == -) {
next[now][i] = next[fail[now]][i]; //如果没有边构造一条边出来
// 构造的边是fail指针指向的节点的出边
} else {
fail[next[now][i]] = next[fail[now]][i]; //有边fail就指向与目前的相同结点(以目前匹配的串的最长后缀为前缀)的一条边
que.push(next[now][i]);
}
}
}
} int query(char s[]) {
int len = strlen(s);
int ans = ;
int now = root;
for(int i = ; i < len; i++) {
now = next[now][s[i] - 'a'];
int tmp = now;
while(tmp != root) {
ans += val[tmp];
val[tmp] = ;
tmp = fail[tmp]; // KMP思想:当前匹配失败,沿着失配边走看有没有能够匹配的串
}
}
return ans;
}
}; char s[];
Ac_DFA ac; int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
ac.init();
for(int i = ; i < n; i++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
scanf("%s", s);
int ans = ac.query(s);
printf("%d\n", ans);
}
return ;
}
HDU 2222:Keywords Search(AC自动机模板)的更多相关文章
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
随机推荐
- WCF第一个Demo
参考文献:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html 自己学习的Demo 第一个是控制台宿主服务,第二个是Windows服 ...
- Uint8Array 对象
8 位无符号整数值的类型化数组.内容将初始化为 0.如果无法分配请求数目的字节,则将引发异常. 语法 uint8Array = new Uint8Array( length ); uint8Array ...
- Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解
定时任务就是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,本文从从JDK自带的一些方法来实现定时任务的需求. 一.Timer和TimerTask Timer和Tim ...
- Web Service测试利器 Postman
利器推荐,下载地址 https://github.com/a85/POSTMan-Chrome-Extension 在chrome的扩展商店中安装失败 所以下载zip文件下来再导入到chrome扩展
- Shell cmd set note
查看启动信息 dmesg [ 0.000000] Initializing cgroup subsys cpuset[ 0.000000] Initializing cgroup subsys cpu ...
- Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-5-6 踩踏平台是怎么炼成的
在游戏中,有很多分来飞去的平台,这个平台长短不一.如果每种长度都去创建一张图片那是比较繁琐的事情.实际上,我们只用到3张图.分别是平台的,平台的中间部分,平台的右边.关键是平台的中间部分,两张中间部分 ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(5)
12.5 创建表和发送查询 介绍了Postgres-XC以及其底层的思想之后,是时候创建我们的第一个表,看看集群将如何表现.下面的例子演示了一个简单的表.将使用id列的哈希键来分布它: test=# ...
- 事件监听addEventListener()和removeEventListener() ---------1
一直想写一个原生事件监听的记录,方便以后看,不愿意写主要是事件监听的单词太长,记起来好难记每次都要查,这次把知道的写完了,来这里查好了,以后要是理解的更透彻了,就再补全好了 首先,DOM0级事件和DO ...
- Adobe Flash CC 安装报错的解决办法
安装FlashCC的时候莫名的报错 ---------------------------Flash.exe - 应用程序错误---------------------------应用程序无法正常启动 ...
- Hibernate Annotation笔记
(1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ...