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 ...
随机推荐
- [转]qt中文乱码问题
http://blog.csdn.net/brave_heart_lxl/article/details/7186631#
- CL.exe的 /D 选项, Preprocessor Macro预处理器宏定义
在看"Inside COM"第10章的代码. MAKEFILE里面有几个标记我没看懂. 去网上搜也搜不到. /D_OUTPROC_SERVER_ /DWIN32 /DREGISTE ...
- iOS UICollectionView之二(垂直滚动)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- iOS UITableView的分割线短15像素,移动到最左边的方法(iOS8)
有好几个朋友问我ios 分割线端了一些 如何解决,于是我就写一篇博客吧.为什么我说是少了15像素呢?首先我们拖拽一个默认的tableview 控件! 看下xcode5 面板的inspector(检查器 ...
- 配置DruidDataSource参考(com.alibaba.druid.pool.DruidDataSource)
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-met ...
- 算法训练 Anagrams问题
http://lx.lanqiao.org/problem.page?gpid=T223 算法训练 Anagrams问题 时间限制:1.0s 内存限制:512.0MB 问题描述 An ...
- ajax常用参数
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.前台跳转到后台 请求参数:前台向后台传数据 回调函数:回调函数就是一个自定义的函数在发生特定的事件的时候调用来处理这个事件 ...
- 简单常用的sql,统计语句,陆续整理添加吧
1. 分段统计分数 if object_id('[score]') is not null drop table [score] go create table [score]([学号] i ...
- Oracle PL/SQL中的循环处理(sql for循环)
今天来说下Oracle中的循环迭代处理,因为从自己的博客统计中看到,不少网友都搜索了关键字"SQL FOR循环",所以打算在这里说下个人的理解. PL/SQL也和我们常用的编程语言 ...
- session_start()一些问题
session问题集锦 对于PHP的session功能,始终找不到合适的答案,尤其是一些错误,还有一些没有错误的结果,最可怕的就是后者,一直为许多的初学者为难.就连有些老手,有时都被搞得莫名其妙.本文 ...