#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. Hibernate配置文件中映射元素详解

    对象标识符号 在关系数据库表中,主键(Primary Key)用来识别记录,并保证每条记录的唯一性.在Java语言中,通过比较两个变量所引用对象的内存地址是否相同,或者比较两个变量引用的对象值是否相同 ...

  2. CSS常见BUG

    CSS Hack IE条件注释: 所有IE:<!--[if IE]> css code <![endif]--> IE6以上:<!--[if gt IE 6]> c ...

  3. PHP 中数组函数 isset 效率比 array_key_exists 更高

    PHP 中数组函数 isset 效率比 array_key_exists 更高 PHP 浏览:4194 2014年04月05日 isset 和 array_key_exists 函数都可以用来测试数组 ...

  4. gulp编译less简单demo

    写个简单的less.watch任务的demo分享———— 1.准备: 安装全局node.npm,这个教程很多不作详细介绍: 安装全局gulp npm install -g gulp 新建getstar ...

  5. SpringMVC–SSH -- RESTful -- JSR303

    最近在使用SpringMVC+MyBatis感觉用起来效果很好.我不太明白SpringMVC和SSH的关系,遂搜索了一下.原来使用SpringMVC之后,可以替代之前的SSH这种开发模式. 附上知乎链 ...

  6. CoreData的数据迁移

    CoreData的数据迁移   很多人说CoreData坑多,现在才感觉到,今天上午写代码的时候,发现了一个大问题. 过程如下,之前我自己给coredata的实体添加了几个字段,后来网上的一个用户说我 ...

  7. HTML5 的段落首行缩进

    text-indent:0em;表示当前行不需要缩进,文本顶头开始.这个属性可以用在  div  p等元素下面 文本首行的缩进(在首行文字之前插入指定的长度) p { line-height: 2em ...

  8. Android Development Tools 发生checkAndLoadTargetData错误

    之前使用时没有出现任何问题的,我把D:\IDE\ADT\adt-bundle-windows-x86_64-20140321\eclipse目录下面的 eclipse.exe重名名为adt.exe并设 ...

  9. Android中读取assets文件夹中的子文件夹内容

    文件结构如下:assets/info/info AssetManager am = this.getResources().getAssets(); InputStream input = null; ...

  10. IP防护等级

    IP(INGRESS PROTECTION)防护等级系统是由IEC(INTERNATIONAL ELECTROTECHNICAL COMMISSION)所起草,将电器依其防尘防湿气之特性加以分级.这里 ...