AC自动机(模板)
#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自动机(模板)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- log在线生成器 html中如何设置浏览器中标题前的logo
制作logo的地址:http://www.logomaker.com.cn/ 设置网站logo的方法 在<head></head>标签之间输入<link rel=&quo ...
- Java之集合类
出处:http://blog.csdn.net/zhangerqing 一.集合类简介 数组是很常用的一种的数据结构,我们用它可以满足很多的功能,但是,有时我们会遇到如下这样的问题: 1.我们需要该容 ...
- AFNetworking实现 断点续传
用AFNetworking实现断点续传,暂停,继续 AFNetworking断点续传暂停恢复 AFNetworking的版本:platform:ios,'7.0' pod "AFNetw ...
- C++之------进制学习
碰到一些寄出的东西不是很理解,就是关于多进制在代码中的转换: 比喻一个数number的多进制表示方法:B:二进制 Q:八进制 D:十进制 H:十六进制 二进制:0bnumber ( ...
- 串口屏之------Usart GPU 使用手册
Usart GPU 使用手册 文档更新日期 更新内容 2014-9-10 C编程sprintf问题 2014-8-8 版本程序1.0,升级了自定义波特率部分 ------ 原始版本 第一部分:基础应用 ...
- 关于Fragment与Activity的想法
View,Fragment,Activity,ListView等都会涉及到Layout文件 不要从Layout来考虑,而是从Activity,Fragment,来考虑,Layout只是他们的一个属性 ...
- 【转】android TV CTS 4.0.3_r1测试
原文网址:http://blog.sina.com.cn/s/blog_539ad34301012764.html 做CTS测试已经一月有余,经过不断的摸索,现总结经验,与大家分享. 一.Downlo ...
- 方案:抵御 不明SSL证书导致的 中间人攻击
基于SSL数字证书的中间人攻击已经不是一个新技术了,但是我发现很多人并不清楚这种威胁,甚至感觉无所谓,我相信他们是由于短暂的无知蒙蔽了双眼,希望这篇文章可以让更多的人知道这种攻击方式,并清除这种网络威 ...
- Makefile中使用foreach生成一类规则
CSDN上,有朋友发帖问了这样一个问题(我按自己的理解翻译一下): 当前目录下有四个静态库文件: liba.a libb.a libc.a libd.a.现在想将它们做成一个动态库libp.so. ...
- LVM(2)逻辑卷的扩展、缩减、快照卷
一.扩展逻辑卷:lvextend 扩展逻辑卷物理边界 -L [+]# /PATH/TO/LV2G, +3G5G