HDU 2222 Keywords Search(AC自动机模板题)
学习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自动机模板题)的更多相关文章
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- 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 ...
随机推荐
- android 内部存储相关知识点: getfilestreampath getDir 子文件夹
文件系统的API的命名方式和常规的不一样: 都是get命名,但是功能就是能创建文件夹... 这种方式的API 命名习惯和常规的不一样... createXXX ----方便查找 http://i ...
- PHP开篇之环境的搭建
PHP开篇之环境的搭建 Wamp软件下载:http://www.wampserver.com/ 此时是2.5版本 下载下来一键安装. 安装有个主意 这里先不用管 或者smtp@qq.com 13643 ...
- CheckBoxList 获取与设置选中的值
/// <summary> ///CheckBoxListHelper 的摘要说明 ///CheckBoxList获取与设置选中的值 /// </summary> public ...
- 使用IntelliJ Idea创建Spring MVC项目
- eclipse myeclipse zend studio 中去掉双引号中的斜体
去windows/preferences 菜单里,选Web/JSP Files/Editor/Syntax Coloring 在右边选Attribute Values ,把Italic的钩去掉就好了.
- myhuiban会议,期刊,科研人员,计算机类会议大全
http://www.myhuiban.com/ List of computer science conferences From Wikipedia, the free encyclopedia ...
- oracle 根据汉字返回拼音函数
参见戴明明的博客,oracle 根据汉字返回拼音函数,由于他的博客里没有提供完整的代码,研究了一个多小时,才弄出来: 上来贴代码吧.. --------------Type Definition CR ...
- bzoj1057,poj3250
bzoj1057本质上是求最大子矩阵: 第一问是一个经典的O(n2)dp 第二问就是最大子矩阵,回眸一下当年卡了我很久的问题: 首先穷举显然不行(这不废话吗?): 首先我们预处理每个点可以最大向上延展 ...
- [原]Unity3D深入浅出 - 物理引擎之刚体部件(Rigidbody)
在虚拟世界中,任何物体都是没有活力的,要想变的真实,Rigidbody是必不可少的组件,下面介绍Rigidbody的各个属性: Mass:质量 Drag:阻力,对象在运动时遇到的空气阻力,0表示没有空 ...
- Welcome to Linux From Scratch!
/**************************************************************************** * Welcome to Linux Fro ...