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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- centos6+cdh5.4.0 离线搭建cdh搭建
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 010 有顺序的Map的实现类:TreeMap和LinkedHashMap
作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...
- MFC中自定义消息
在头文件stdafx.h中增加一个自定义消息宏 #define WM_USER_THREADEND WM_USER + 1 在于增加新消息的窗口或对话框类的头文件中增加一个回调函数声明 afx_msg ...
- [国嵌攻略][052][NandFlash驱动设计_读]
NandFlash读数据方式 1.页读,读出页中主数据区的所有数据,提供页地址(行地址) 2.随机读,读出页中指定的存储单元的数据,提供页地址(行地址)和页内偏移(行地址) 代码编写 1.根据Nand ...
- linux 下CentOS 下 npm命令安装gitbook失败的问题
运行环境 linux 服务器:CentOS 7.0 系统:安装了nodejs :使用 npm 安装 gitbook 出现错误提示: npm install -g gitbook-cli symbol ...
- the server responded with a status of 414 (Request-URI Too Large)
nginx 配置不当,前端ajax调用报错: the server responded with a status of 414 (Request-URI Too Large) 浏览器F12报错如下: ...
- Java数据结构和算法(十三)——哈希表
Hash表也称散列表,也有直接译作哈希表,Hash表是一种根据关键字值(key - value)而直接进行访问的数据结构.它基于数组,通过把关键字映射到数组的某个下标来加快查找速度,但是又和数组.链表 ...
- Android知识点剖析系列:深入了解layout_weight属性
摘录自:http://www.cnblogs.com/net168/p/4227144.html 前言 Android中layout_weight这个属性对于经常捣鼓UI的我们来说,肯定不会陌生.但是 ...
- React源码解析:setState
先来几个例子热热身: ......... constructor(props){ super(props); this.state = { index: 0 } } componentDidMount ...
- SpringMVC之GET请求参数中文乱码
server.xml 文件中的编码过滤器设置是针对POST请求的,tomacat对GET和POST请求处理方式是不同的,要处理针对GET请求的编码问题,则需要改tomcat,conf目录下的serve ...