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自动机模板)的更多相关文章

  1. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  2. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  3. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  4. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  5. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  6. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  7. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  8. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  9. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

随机推荐

  1. Xib文件的使用

  2. js markdown chart flow

    http://knsv.github.io/mermaid/#example-of-a-marked-renderer

  3. grunt搭建前端自动化实践

    grunt是什么? grunt是一个前端构建工具, 每种应用开发, 都有一套构建工具, 例如linux c程序开发, 构建工具是make, java程序的构建工具为maven,web前端经过十多年的发 ...

  4. java 笔记(5) —— 线程,yield,join

    一.线程各个状态与转换: 新建状态:用new语句创建的线程对象处于新建状态,此时它和其它的java对象一样,仅仅在堆中被分配了内存 .就绪状态:当一个线程创建了以后,其他的线程调用了它的start() ...

  5. SqlServer跨库查询

    由于业务的拆分,数据库拆分为两种作用: 汇总数据库(Master,头节点数据库), 子节点数据库(Compute Node,计算子节点数据库) 这样,就设计到子节点访问头节点数据库中的某张汇总表,这种 ...

  6. 20145207 《Java程序设计》第5周学习总结

    前言:先聊两句,上午电路实习,刚开始没多久就让电烙铁烫了,倒霉催的~晚上来这里接着弄代码,透心凉心飞扬~ 教材学习内容总结 一.异常处理 1.语法与继承结构 使用try.catch: Java中所有错 ...

  7. [转]JVM内幕:Java虚拟机详解

    本文由 ImportNew - 挖坑的张师傅 翻译自 jamesdbloom.欢迎加入翻译小组.转载请见文末要求. 这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 ...

  8. [原创]spring学习笔记:关于springsource-tool-suite插件的安装

    1.首先我们得确定自己使用的eclipes的版本,具体方式:打开eclipes > help > About Eclipes > 点击eclipes的logo > 查看ecli ...

  9. PHP-字符串过长不用担心

    字符串过长可以用定界符: >>>GOD ................巨长务必的字符串 GOD;

  10. Fresco源码解析 - DataSource怎样存储数据

    Fresco源码解析 - DataSource怎样存储数据 datasource是一个独立的 package,与FB导入的guava包都在同一个工程内 - fbcore. datasource的类关系 ...