询问有多少个模式串出现在了文本串里面。将模式串插入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. UVA 10891 Game of Sum(区间DP(记忆化搜索))

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. 区间DP小结

    也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...

  3. Centos简介

    Centos作为主流的一种Linux操作系统,以后项目中,比如后期Redis,以及部署一些项目,会把Centos作为服务器操作系统,我们选用Centos,主要是免费,以及稳定. Centos详细介绍, ...

  4. GreenPlum学习笔记:基础知识

    一.介绍 GreenPlum分布式数据仓库,大规模并行计算技术. 无共享/MPP核心架构 Greenplum数据库软件将数据平均分布到系统的所有节点服务器上,所以节点存储每张表或表分区的部分行,所有数 ...

  5. django 建立一个简单的应用

    本人的用的版本是python 2.7.3和django 1.10.5,Windows10系统 1.首先通过命令建立项目和app 找到django的安装路径,我的路径是:C:\Python27\Lib\ ...

  6. Windows开机自动启动pageant,方便使用ssh链接到GitHub

    按win +r,输入 shell:startup "C:\Program Files\TortoiseGit\bin\pageant.exe" "d:\GitHubPri ...

  7. Java第三阶段学习(二、IO流--------递归,字节流Stream)

    一.递归 定义:指方法在方法内调用自己 适用于方法的运算主体不变,但运行的时候,参与运算的方法参数会变化注意:一定要给递归一个出口,否则内存溢出 练习题1:使用递归打印文件夹中所有的文件,包含子目录中 ...

  8. java把html标签字符转换成普通字符(反转换成html标签)

    package net.jasonjiang.web; import org.junit.Test; import org.springframework.web.util.HtmlUtils; /* ...

  9. A - Superset CodeForces - 97B(人生第一个分治法,感觉,像二分啊。。)

    /* 分治法,第一次做不是很懂,借鉴了神犇代码,但实操之后感觉像二分,,可能做得少了或者就是.... */ 题目大意: 一个集合里有若干点,要求你添加某些点后保证这个集合里的任意两点满足以下三个条件中 ...

  10. 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心

    P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...