学习AC自动机请戳这里:大神blog........

自动机的模板:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 100005
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define L(x) x<<1
#define R(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
using namespace std; struct trie {
trie *fail; //失败指针
trie *next[26];
int cnt;
trie () {
fail = 0;cnt = 0;
memset(next,0,sizeof(next));
}
}*q[511111]; //模拟队列
trie *rt;
int head,tail;
char keyword[51];
char book[1111111]; void insert(char *key) {
trie *p = rt;
int t;
while(*key) {
t = *key - 'a';
if(p->next[t] == NULL) p->next[t] = new trie();
p = p->next[t];
key++;
}
p->cnt++; //表示一个单词
} void bfs() {
rt->fail = NULL; //根结点fail指向空
q[head++] = rt;
while(head != tail) {
trie *t = q[tail++];
trie *p = NULL;
for(int i=0; i<26; i++) {
if(t->next[i] != NULL) { //对所有儿子的fail指针匹配并且入队
if(t == rt) t ->next[i]->fail = rt; //如果刚从根节点出发
else { //否则沿着他父亲的失败指针走,
//直到走到一个节点,他的儿子中也有相同字符的节点。然后把当前节点的失败指针指向他的那个儿子
p = t->fail;
while(p != NULL) {
if(p->next[i] != NULL) {
t->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(p == NULL) t->next[i]->fail = rt; //如果一直走到了root都没找到,那就把失败指针指向root
}
q[head++] = t->next[i];
}
}
}
} int query(char *key) {
trie *p = rt;
int cnt = 0;
while(*key) {
int t = *key - 'a';
while(p->next[t] == NULL && p != rt) p = p->fail; //如果当前字符不匹配
p = p->next[t];
if(p == NULL) p = rt; //最终还是未匹配
trie *tmp = p;
while(tmp != rt && tmp->cnt != -1) {
cnt += tmp->cnt;
tmp->cnt = -1; //该处已经出现过了
tmp = tmp->fail;
}
key++;
}
return cnt;
}
int main(){
int T;
cin >> T;
while(T --) {
rt = new trie();
int n;
cin >> n;
for(int i=0; i<n; i++) {
scanf("%s",keyword);
insert(keyword);
}
head = 0; tail = 0;
bfs();
scanf("%s",book);
printf("%d\n",query(book));
}
return 0;
}

HDU 2222 Keywords Search(AC自动机模板题)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  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 ...

随机推荐

  1. 安装Hadoop系列 — 安装Eclipse

    1.下载 Eclipse从 http://www.eclipse.org/downloads/index-developer.php下载合适版本,如:Eclipse IDE for C/C++ Dev ...

  2. BIG5编码表

    Big5 (Traditional Chinese) character code table code +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F ...

  3. 【HDOJ】4358 Boring counting

    基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...

  4. windows官方多语言方案

    编写 Win32 多语言用户界面应用程序 Windows 2000 针对全球市场制定了新的增强支持标准,提供了许多国际化功能,例如完全支持 Unicode.预设支持数百种语言以及用于从右向左语言的镜像 ...

  5. 【js】获得项目路径

    var curWwwPath=window.document.location.href; //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp var pathNam ...

  6. 【转】【iOS】导航栏那些事儿

    原文网址:http://www.jianshu.com/p/f797793d683f 参考文章 navigationItem UINavigationItem UINavigationBar UIBa ...

  7. ImageMagick的使用

    关于ImageMagick ImageMagick (TM) 是一个免费的创建.编辑.合成图片的软件.它可以读取.转换.写入多种格式的图片.图片切割.颜色替换.各种效果的应用,图片的旋转.组合,文本, ...

  8. tomcat server.xml配置详解

    由于 Tomcat 基于 Java,实际上在各种 Linux 发行版里的配置方法都大同小异,只是我看见在 Arch Linux 环境里搭建 Tomcat 的文章比较少,所以在 Arch Linux 实 ...

  9. CSS3 Transitions, Transforms和Animation使用简介与应用展示

    CSS3 Transitions, Transforms和Animation使用简介与应用展示 by zhangxinxu from http://www.zhangxinxu.com本文地址:htt ...

  10. 异常处理 Exception

    一.异常类 1.在C#中所有的异常都是使用一个异常类型的示例对象表示的,这些异常类型都是继承自System.Exception类型,或者直接使用System.Exception类型的实例对象: 2.在 ...