#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; int n, ans[ + ];
char str[][ + ]; struct AcAutomaton{
static const int N = + ;
static const int C = ; int size;
int ch[N][C], fail[N], num[N], end[N]; AcAutomaton(){
size = ;
} int idx(char c){
return c - 'a';
} void insert(char *buf, int k){
int cur = , len = strlen(buf); for(int i = ; i < len; ++ i){
int x = idx(buf[i]); if(!ch[cur][x])
ch[cur][x] = size ++;
cur = ch[cur][x];
} end[cur] ++; num[cur] = k;
} void build(){
queue <int> q;
fail[] = ; for(int i = ; i < C; ++ i){
if(!ch[][i]) ch[][i] = ;
else{
fail[ch[][i]] = ;
q.push(ch[][i]);
}
} while(!q.empty()){
int x = q.front(); q.pop(); for(int i = ; i < ; ++ i){
if(!ch[x][i]) ch[x][i] = ch[fail[x]][i];
else{
fail[ch[x][i]] = ch[fail[x]][i];
q.push(ch[x][i]);
}
}
}
} void find(char* buf){
int cur = , len = strlen(buf); for(int i = ; i < len; ++ i){
int x = idx(buf[i]); cur = ch[cur][x]; int tmp = cur;
while(tmp){
if(end[tmp])
ans[num[tmp]] += end[tmp];
tmp = fail[tmp];
}
}
}
}ac; int main(){
#ifndef ONLINE_JUDGE
freopen("ACautomata.in", "r", stdin);
freopen("ACautomata.out", "w", stdout);
#endif scanf("%d", &n);
for(int i = ; i <= n; ++ i){
scanf("%s", str[i]);
ac.insert(str[i], i);
}
ac.build();
scanf("%s", str[n+]);
ac.find(str[n+]); for(int i = ; i <= n; ++ i){
int tp = strlen(str[i]);
for(int j = ; j < tp; ++ j)
printf("%c", str[i][j]);
printf(" %d\n", ans[i]);
} #ifndef ONLINE_JUDGE
fclose(stdin); fclose(stdout);
#endif return ;
}

AcAutomaton

AC自动机(模板)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

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

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

  8. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  9. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  10. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

随机推荐

  1. Lucene文件扩展名

    名称 文件后缀 描述 段文件(Segments File) segments.gen segments_N 存储提交点信息 锁文件(Lock File) write.lock 用来阻止多个indexW ...

  2. js中:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. c++ 编译期计算 (一)

    编译期就是编译器进行编译,产生.obj文件的所处的那一段时间(如果是广义的编译期,那么一般还包括了链接期,因为现在很多编译器都会自动调用链接器进行链接)执行期就是你执行某个已经链接好的程序的那段时间. ...

  4. HDU 3966 dfs序+LCA+树状数组

    题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...

  5. jquery 1.9 之后toggle不能用的问题

    今天用到toggle这个方法,发现不是自己想要的效果,之前有用过好多次,一直都没有问题. 网上查了原因,才知道是版本的问题,jquery1.9之后toggle取消了.那么如果想要继续用toggle的这 ...

  6. php的一些小笔记-文件函数(2)

    ---恢复内容开始--- copy 文件的复制 echo copy('test.php','test1.php'); 如果成功的返回true,反之返回false 如何在多层目录中复制文件呢?也就是根据 ...

  7. python文件_改名2

    #手动选择路径,批量改名 import os,re,time,tkFileDialog global i #文件名后面增加后缀:txt,png,bng,jpeg,jpg,gif,zip类型的文件 de ...

  8. 写下你的第一个Django应用,第三部分

    这篇指南开始于指南2结束的地方.我们将继续web投票应用和集中注意力在创建公共接口——“view” 理念 一个视图在你的Django应用中一个web页面的“品种”和它通常作为一个特定的函数以及有一个特 ...

  9. Java之集合类

    出处:http://blog.csdn.net/zhangerqing 一.集合类简介 数组是很常用的一种的数据结构,我们用它可以满足很多的功能,但是,有时我们会遇到如下这样的问题: 1.我们需要该容 ...

  10. 爬虫框架YayCrawler

    爬虫框架YayCrawler 各位好!从今天起,我将用几个篇幅的文字向大家介绍一下我的一个开源作品——YayCrawler,其在GitHub上的网址是:https://github.com/liush ...