HDU 2222 Keywords Search 【AC自动机模板】
询问有多少个模式串出现在了文本串里面。将模式串插入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自动机模板】的更多相关文章
- 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 ...
随机推荐
- jersey中的405错误 method not allowed
- HDU 1507 Uncle Tom's Inherited Land(最大匹配+分奇偶部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 题目大意:给你一张n*m大小的图,可以将白色正方形凑成1*2的长方形,问你最多可以凑出几块,并输 ...
- putIfAbsent
public static HashSet<Long> getOrInitHashMapCacheValue(Long mId){ // HashSet<Long> set = ...
- java批量生成excel文件
1.导入用于操作excel的jar,地址:https://pan.baidu.com/s/1qXADRlU 2.生成excel使用的模版文件,地址:https://pan.baidu.com/s/1c ...
- 从exp入手分析漏洞
分析poc和分析exp有一些不一样,因为exp是人为构造后的东西,它会执行一段自定的shellcode.结果是根本不会触发异常或者异常在离触发点十万八千里的地方.这样分析poc的技巧就用不上了(因为无 ...
- vue-cli中如何集成UEditor 富文本编辑器?
1.根据后台语言下载对应的editor插件版本 地址:https://ueditor.baidu.com/website/download.html 2.将下载好的资源放在/static/uedito ...
- wordpress 常用函数-wpdb类
与数据库建立接口 WordPress为用户提供了一系列用于数据库操作的函数类——wpdb.Wpdb类建立在Justin Vincent编写并维护的ezSQL类的基础上. 使用须知 不可直接调用wpdb ...
- 安装部署Apache Hadoop (本地模式和伪分布式)
本节内容: Hadoop版本 安装部署Hadoop 一.Hadoop版本 1. Hadoop版本种类 目前Hadoop发行版非常多,有华为发行版.Intel发行版.Cloudera发行版(CDH)等, ...
- 【51nod】1934 受限制的排列
题解 这题还要判无解真是难受-- 我们发现我们肯定能确定1的位置,1左右的两个区间是同理的可以确定出最小值的位置 我们把区间最小值看成给一个区间+1,构建出笛卡尔树,就求出了每一次取最小值和最小值左右 ...
- JS日期、时间 格式化转换方法
Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...