AC 自动机 模板
简单版
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
const int MAXN=1000005;
int n;
char s[MAXN];
struct ACAM{
int nxt[MAXN][26],end[MAXN],fail[MAXN],nume;
ACAM(){
memset(nxt,0,sizeof(nxt));
memset(end,0,sizeof(end));
memset(fail,0,sizeof(fail));
nume=0;
}
void ins(char s[]){
int len=strlen(s);
int p=0;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(!nxt[p][v]) nxt[p][v]=++nume;
p=nxt[p][v];
}
end[p]++;
}
void getfail(){
queue <int>q;
for(int i=0;i<26;i++) if(nxt[0][i]) fail[nxt[0][i]]=0,q.push(nxt[0][i]);
int p=0;
while(!q.empty()){
p=q.front();q.pop();
for(int i=0;i<26;i++){
if(nxt[p][i]) fail[nxt[p][i]]=nxt[fail[p]][i],q.push(nxt[p][i]);
else nxt[p][i]=nxt[fail[p]][i];
}
}
}
int query(char s[]){
int len=strlen(s);
int p=0,ans=0;
for(int i=0;i<len;i++){
p=nxt[p][s[i]-'a']; //注意这里是s[i]-'a'
for(int k=p;k&&(~end[k]);k=fail[k]) ans+=end[k],end[k]=-1;
}
return ans;
}
}AC;
int main(){
freopen("in.txt","r",stdin);
cin>>n;
for(int i=1;i<=n;i++) scanf("%s",s),AC.ins(s);
AC.getfail();
scanf("%s",s);int ans=AC.query(s);
printf("%d\n",ans);
fclose(stdin);
return 0;
}
增强版
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#define RST(a) memset((a),0,sizeof((a)))
using namespace std;
const int MAXN=1e6+10,MAXM=152*72;
int n;
char s[152][72],t[MAXN];
struct ACAM{
int nxt[MAXM][26],fail[MAXM],end[MAXM],nume,cnt[MAXM],lst[MAXN];
bool f[MAXM];
void clear(){
RST(nxt);RST(fail),RST(end),RST(cnt);nume=0;RST(lst);
}
void ins(char s[],int index){
int len=strlen(s);
int p=0;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(!nxt[p][v]) nxt[p][v]=++nume;
p=nxt[p][v];
}
end[p]=index;
}
void getfail(){
queue <int> q;
for(int i=0;i<26;i++) if(nxt[0][i]) fail[nxt[0][i]]=0,q.push(nxt[0][i]);
int p=0;
while(!q.empty()){
p=q.front(),q.pop();
for(int i=0;i<26;i++){
int v=nxt[p][i];
if(v) {
fail[v]=nxt[fail[p]][i],q.push(v);
lst[v]=end[fail[v]]?fail[v]:lst[fail[v]];
}
else nxt[p][i]=nxt[fail[p]][i];
}
}
}
int query(char t[]){
int len=strlen(t);
int p=0;
for(int i=0;i<len;i++){
p=nxt[p][t[i]-'a'];
if(end[p]) cnt[end[p]]++;
for(int k=lst[p];k;k=lst[k]) cnt[end[k]]++;
}
int res=0;
for(int i=1;i<=n;i++) res=max(res,cnt[i]);
printf("%d\n",res);
for(int i=1;i<=n;i++) if(cnt[i]==res) printf("%s\n",s[i]);
}
}AC;
int main(){
freopen("in.txt","r",stdin);
scanf("%d",&n);
while(n){
AC.clear();
for(int i=1;i<=n;i++){
scanf("%s",s[i]);//cout<<s[i]<<endl;
AC.ins(s[i],i);
}
AC.getfail();
scanf("%s",t);
AC.query(t);
scanf("%d",&n);
}
fclose(stdin);
return 0;
}
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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- 关于解决Git项目本地修改代码之后执行pull操作之后报错的问题
解决办法: 注意!该方法执行后会导致远程仓库覆盖本地仓库的文件,如果不需要对本地文件进行保存,可以无视,若之后还需要用到,请备份所报错文件! 1.Eclipse中选中项目右键-->Team--& ...
- 解决vi编辑器不能使用方向键和退格键问题的两种方法
方法1.使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母? 在Ubuntu中,进入vi命令的编辑模式,发现按方向键不能移动光标,而是会输出ABCD,以及退格键也不能正常删除字符.这是由于 ...
- nth-child()选择器小结
之前也用到过nth-child,只是当时理解的不够透彻.今天回过头去看这个知识点时,发现了一个易错点. 浏览器支持情况: 所有主流浏览器都支持nth-child()选择器,除了IE8及更早的版本. 下 ...
- ZOJ 1203 Swordfish
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- CUDA与OpenGL互操作
当处理较大数据量的时候,往往会用GPU进行运算,比如OpenGL或者CUDA.在实际的操作中,往往CUDA实现并行计算会比OpenGL更加方便,而OpenGL在进行后期渲染更具有优势.由于CUDA中的 ...
- NSString拼接字符串
NSString* string; // 结果字符串 02 NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来 03 04 / ...
- web.xml 报错
1.The markup in the document following the root element must be well-formed. 原因是配置时没有 放在根下 <web-a ...
- CentOS 7安装Oracle 11gR2以及设置自启动
一.环境准备 1.正确无误的CentOS 7系统环境 CentOS 7安装:http://www.cnblogs.com/VoiceOfDreams/p/8043958.html 2.正确的JDK环境 ...
- RGB颜色设置错误
[UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:& ...
- 2017-06-28(passwd 主要组与附属组 gpasswd newgrp groups)
passwd passwd -l 用户名 (锁定用户) passwd -u 用户名 (解锁用户) passwd -d 用户名 (清楚用户密码) 主要组与附属组 一个用户可以同时属于多个组,其中一个 ...