询问有多少个模式串出现在了文本串里面。将模式串插入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++ 实现memcpy和strcpy

    /** * @Method: Memcpy * @Access: public * @Return: void * * @Param : dst - 目的起始地址 * @Param : src - 源 ...

  2. nginx重写链接

    location ~ /send.redir {             if ( $query_string ~ “url=(?<path>.+)” ) {rewrite ^.* htt ...

  3. 日志、字段备注查询、自增ID联系设置、常用存储过程

    -----获取数据字典SQL(表字段说明)SELECT     [Table Name] = OBJECT_NAME(c.object_id),     [Column Name] = c.name, ...

  4. 编程六月定律 | 外刊IT评论网

    编程六月定律 上周,我被迫对一个很老的项目做一些修改.麻烦是,当开始着手时,我真的记不清这个项目究竟有多老了. 这实际上是我使用Codeigniter实现的第一个MVC项目.打开项目文件后,很多东西都 ...

  5. 【读书笔记】Android平台的漏洞挖掘和分析

    最近比较关注移动端的安全,以后也打算向安卓平台的安全发展.这篇博文主要是记录一些研究Android安全的读书笔记. Fuzzing技术的核心是样本生成技术 测试Android平台的组件间通信功能使用的 ...

  6. OpenCV 颜色空间转换参数CV_BGR2GRAY改变

    OpenCV的颜色空间转换函数:   C++: void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )   参数d ...

  7. 使用PHP写了一个图片分割等份工具,便于前台页面切图时使用。

    目的: 由于网站更新活动较频繁,其大多数以静态图片为主,设计人员在除了设计图后都要给前端制作人员再次切图从而达到页面加载图片缓慢的问题,为了减少工作量做了该工具. 功能: 上传一张图,将其分割成指定等 ...

  8. #define const typedef

    #define用法 1. 定义简单的常数:定义常量,便于修改 #define N 1000 2. 定义简单的函数:注意多使用括号 define可以像函数那样接受一些参数,如下: #define max ...

  9. Bootstrap Paginator分页插件使用示例

    最近做的asp.netMVC项目中需要对数据列表进行分类,这个本来就是基于bootstrap开发的后台,因此也就想着bootstrap是否有分页插件呢,或者说是基于jquery支持的分页功能,这样整体 ...

  10. matlab .fig转化成pdf 缺失

    1. 在matlab figure里面,Edit -> Copy Figure 2. 粘贴到ppt中即可