询问有多少个模式串出现在了文本串里面。将模式串插入Trie树中,然后跑一边AC自动机统计一下就哦了。

献上一份模板。

#include <cstdio>
#include <cstring>
#include <queue>
#define MAX_NODE 240005
#define MAX_CHILD 26
using namespace std; class AC_Automaton {
public:
int chd[MAX_NODE][MAX_CHILD];
int fail[MAX_NODE];
int val[MAX_NODE];
int ID[128];
int sz;
queue<int> q; AC_Automaton() {
for (int i = 0; i < 26; i++)
ID[i + 'a'] = i;
Clear();
} void Clear() {
memset(chd, 0, sizeof (chd));
memset(fail, 0, sizeof (fail));
memset(val, 0, sizeof (val));
sz = 1;
} void Insert(const char *s) {
int cur = 1;
for (int i = 0; s[i]; i++) {
if (!chd[cur][ID[s[i]]]) chd[cur][ID[s[i]]] = ++sz;
cur = chd[cur][ID[s[i]]];
}
val[cur]++;
} void Build_AC() {
while (!q.empty()) q.pop();
q.push(1);
fail[1] = 1;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = 0; i < MAX_CHILD; i++)
if (chd[cur][i]) {
if (cur == 1) fail[chd[cur][i]] = 1;
else {
int tmp = fail[cur];
while (tmp != 1 && chd[tmp][i] == 0) tmp = fail[tmp];
if (chd[tmp][i]) fail[chd[cur][i]] = chd[tmp][i];
else fail[chd[cur][i]] = 1;
}
q.push(chd[cur][i]);
}
}
} int Query(const char *s) {
int ret = 0;
int cur = 1, tmp;
for (int i = 0; s[i]; i++) {
if (chd[cur][ID[s[i]]]) cur = chd[cur][ID[s[i]]];
else {
while (cur != 1 && chd[cur][ID[s[i]]] == 0) cur = fail[cur];
if (chd[cur][ID[s[i]]]) cur = chd[cur][ID[s[i]]];
}
tmp = cur;
while (tmp != 1 && val[tmp] != -1) {
ret += val[tmp];
val[tmp] = -1;
tmp = fail[tmp];
}
}
return ret;
}
} AC; char s[60], text[1000001]; int main() {
int T, n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
AC.Clear();
while (n--) {
scanf(" %s", s);
AC.Insert(s);
}
AC.Build_AC();
scanf(" %s", text);
printf("%d\n", AC.Query(text));
}
return 0;
}

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. 读书笔记--C陷阱与缺陷(四)

    第四章 1. 连接器 C语言的一个重要思想就是分别编译:若干个源程序可在不同的时候单独进行编译,恰当的时候整合到一起. 连接器一般与C编译器分离,其输入是一组目标模块(编译后的模块)和库文件,输出是一 ...

  2. mongod 一些命令汇总

    1. 导出数据库: mongoexport -d master -c reports -o no.json --type json -f "title,name" -q '{&qu ...

  3. C压缩字符串中的空格

    使用纯C语言,去除一个字符串开头和结尾的空格,内部若有连续空格只保留一个.  C Code  12345678910111213141516171819202122232425262728293031 ...

  4. Unix IPC之基于共享内存的计数器

    目的 本文主要实现一个基于共享内存的计数器,通过父子进程对其访问. 本文程序需基于<<Unix网络编程-卷2>>的环境才能运行.程序中大写开头的函数为其小写同名函数的包裹函数, ...

  5. 常用sql 全记录(添加中)

    -- 数据库SQL总结中........... --SQL分类: (CREATE,ALTER,DROP,DECLARE) ---DDL—数据定义语言(SELECT,DELETE,UPDATE,INSE ...

  6. sublime text配置make工具

    sublime text配置make工具 Linux下许多项目是用makefile来管理的,是用gcc+make等方式来编译和运行. 在只有tty的场合或年代,使用vim或emacs是不二选择:但在L ...

  7. 【LOJ】#2290. 「THUWC 2017」随机二分图

    题解 看了一眼觉得是求出图对图统计完美匹配的个数(可能之前做过这样模拟题弃疗了,一直心怀恐惧... 然后说是统计一下每种匹配出现的概率,也就是,当前左边点匹配状态为S,右边点匹配状态为T,每种匹配出现 ...

  8. USACO 4.3 Street Race

    Street RaceIOI'95 Figure 1 gives an example of a course for a street race. You see some points, labe ...

  9. 2017-2018-1 20179202《Linux内核原理与分析》第七周作业

    一 .Linux内核创建一个新进程的过程 1. 知识准备 操作系统内核三大功能是进程管理,内存管理,文件系统,最核心的是进程管理 linux 进程的状态和操作系统原理的描述进程状态有所不同,比如就绪状 ...

  10. react篇章-事件处理

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...