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 ...
随机推荐
- adb开启不了解决方案
原文地址: adb开启不了解决方案 - vaecer - 博客频道 - CSDN.NET http://blog.csdn.net/vaecer/article/details/45894643 ...
- 【剑指offer】判断二叉树是否为平衡二叉树
2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...
- Oracle中Blob和Clob类型的区别与操作
Oracle中Blob和Clob类型 1.Oracle中Blob和Clob类型的区别 BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的 ...
- myhuiban会议,期刊,科研人员,计算机类会议大全
http://www.myhuiban.com/ List of computer science conferences From Wikipedia, the free encyclopedia ...
- Hibernate映射之实体映射<转载>
实体类与数据库之间存在某种映射关系,Hibernate依据这种映射关系完成数据的存取,因此映射关系的配置在Hibernate中是最关键的.Hibernate支持xml配置文件与@注解配置两种方式.xm ...
- Android反编译教程
本文摘自 http://blog.csdn.net/ithomer/article/details/6727581 本文Android反编译教程,测试环境: Win7 Ultimate x64 Ubu ...
- BZOJ3155: Preprefix sum
题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...
- UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)
题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...
- Apache2.2+Tomcat7.0整合配置详解
一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Lin ...
- 使用busybox制作rootfs
Build Busybox as a static binary(no shared libs),如果选择上,则busybox将以静态形式进行编译,否则将以动态方式编译.此外,还需要对交叉编译环境进行 ...