学习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. Servlet课程0425(五) sendRedirect实现不同页面共享数据

    Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...

  2. perl next和last

    跳出控制结构:next和last next 和last 操作符运维你在循环中改变程序执行的方向,你可能经常会遇到一些的特殊情况, 碰到这种情况时你希望跳过它,或者像退出循环. 比如当你处理Unix 账 ...

  3. ubuntu终端颜色配置

    对于刚接触ubuntu的同学们,打开终端(ctrl+alt+T),会发现里面都是一个颜色,不管是用户名.主机名还是命令都是白色,当然,用 ls 列出文件的时候是会多一种颜色的.即使这样,对开发人员来说 ...

  4. chrome禁用某个网站js脚本的执行

      1 首先打开谷歌浏览器.如下 2 点击右上角,打开菜单进入[设置] 3 打开后,第一个界面是没有这个的,要滚动到最后点击[显示高级设置...] 4 展开第二页后,点击[隐私设置]->[内容设 ...

  5. poj 2253 Frogger(最短路 floyd)

    题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...

  6. [swustoj 1092] 二分查找的最大次数

    二分查找的最大次数(1092) 问题描述 这里是一个在排序好的数组A(从小到大)中查找整数X的函数,返回值是查找次数. int binarySearch(inta[],int n,int x)//数组 ...

  7. HDU 4571 Travel in time ★(2013 ACM/ICPC长沙邀请赛)

    [题意]给定N个点,每个点有一个停留所需的时间Ci,和停留能够获得的满意度Si,有M条边,每条边代表着两个点走动所需的时间ti,现在问在规定的T时间内从指定的一点S到E能够获得的最大的满意度是多少?要 ...

  8. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.8

    For any matrix $A$ the series $$\bex \exp A=I+A+\frac{A^2}{2!}+\cdots+\frac{A^n}{n!}+\cdots \eex$$ c ...

  9. Spring注释@Qualifier

    在学习@Autowired的时候我们已经接触到了@Qualifier, 这节就来详细学习一下自定义@Qualifier. 例如定义一个交通工具类:Vehicle,以及它的子类Bus和Sedan. 如果 ...

  10. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...