「模板」AC自动机
说明
这篇博客只挂模板,具体分析请膜拜大佬 hyfhaha 大佬。
普通版本
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned
#ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
const int MAXN=1e6;
const int MAXK=26;
int trie[MAXN+5][MAXK+5],flg[MAXN+5],ncnt=1;//注意 ncnt 的初值
int fail[MAXN+5];
inline void Insert(const char* c){
int len=strlen(c),i=-1,p=1;
while(++i<len){
if(!trie[p][c[i]-'a'])trie[p][c[i]-'a']=++ncnt;
p=trie[p][c[i]-'a'];
}
++flg[p];
}
queue<int>Q;
inline void getFail(){
rep(i,0,MAXK)trie[0][i]=1;
while(!Q.empty())Q.pop();
Q.push(1);fail[1]=0;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=0,v,fafail=fail[u];i<MAXK;++i){
v=trie[u][i];
if(!v){trie[u][i]=trie[fafail][i];continue;}
fail[v]=trie[fafail][i];
Q.push(v);
}
}
}
inline int Compare(const char* s){
int len=strlen(s),i=-1,ret=0,p=1;
while(++i<len){
p=trie[p][s[i]-'a'];
for(int k=p;k>1 && flg[k]>=0;k=fail[k])
ret+=flg[k],flg[k]=-1;
}
return ret;
}
int n;
char s[MAXN+5];
signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
for(int i=n=qread();i>0;--i){
scanf("%s",s);
Insert(s);
}
getFail();
scanf("%s",s);
int ans=Compare(s);
writc(ans,'\n');
return 0;
}
询问更改版
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned
#ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
const int MAXN=150;
const int MAXS=70;
const int MAXK=26;
const int MAXL=1e6;
const int MAXSIZE=1e6;
int trie[MAXSIZE+5][MAXK+5],ncnt;
int fail[MAXSIZE+5],enz[MAXSIZE+5];
//失配指针, 以及这个节点的结尾单词
char S[MAXN+5][MAXS+5];//模式串
char T[MAXL+5];//文本串
int tot[MAXN+5];//输出答案用
inline void Init(){
//采用加点时自动更新的方式, 所以此处并没有其他数组的清空
ncnt=1;
rep(i,0,MAXK)trie[0][i]=trie[1][i]=0;
}
inline void Newnode(const int i){
fail[i]=enz[i]=0;
rep(t,0,MAXK)trie[i][t]=0;
}
inline void Insert(const char* ss,const int id){
int len=strlen(ss),p=1,i=-1,v;
while(++i<len){
v=ss[i]-'a';
if(!trie[p][v])Newnode(trie[p][v]=++ncnt);
p=trie[p][v];
}
enz[p]=id;
}
queue<int>Q;
inline void Getfail(){
rep(i,0,MAXK)trie[0][i]=1;
while(!Q.empty())Q.pop();
Q.push(1);fail[1]=0;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=0,v,ff=fail[u];i<MAXK;++i){
v=trie[u][i];
if(!v){trie[u][i]=trie[ff][i];continue;}
fail[v]=trie[ff][i];
Q.push(v);
}
}
}
inline void Compare(const char* ss){
int len=strlen(ss),p=1,i=-1;
while(++i<len){
p=trie[p][ss[i]-'a'];
for(int k=p;k>1;k=fail[k])if(enz[k])
++tot[enz[k]];
}
return;
}
int N;
signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
while(2333333){
Init();
scanf("%d",&N);
if(N==0)break;
rep(i,1,N){
scanf("%s",S[i]);
tot[i]=0;
Insert(S[i],i);
}
Getfail();
scanf("%s",T);
Compare(T);
int maxx=-1;
rep(i,1,N)maxx=Max(maxx,tot[i]);
writc(maxx,'\n');
rep(i,1,N)if(tot[i]==maxx)printf("%s\n",S[i]);
}
return 0;
}
问题:\(trie\) 树的数组针对这道题不应该是 \(26^{70}\) 那么大吗?为什么开 \(1e6\) 就可以过?
拓扑优化版本
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned
#ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
const int MAXN=2e5;
const int MAXSIZE=2e5;
const int MAXL=2e6;
const int MAXK=26;
char S[MAXSIZE+5],T[MAXL+5];
int tot[MAXN+5];
int trie[MAXSIZE+5][MAXK+5],fail[MAXSIZE+5],ncnt=1,enz[MAXN+5];
//此处的 enz 记录第 i 个模式串的对应点
//注意 ncnt 的初值应赋为 1
int in[MAXSIZE+5];//拓扑优化时使用
inline void Insert(const char* ss,const int id){
int p=1;
for(int i=0,siz=strlen(ss),v;i<siz;++i){
v=ss[i]-'a';
if(!trie[p][v])trie[p][v]=++ncnt;
p=trie[p][v];
}
enz[id]=p;//记录对应点
}
inline void Getfail(){
queue<int>Q;
rep(i,0,MAXK)trie[0][i]=1;
Q.push(1);fail[1]=0;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=0,v,ff=fail[u];i<MAXK;++i){
v=trie[u][i];
if(!v){trie[u][i]=trie[ff][i];continue;}
fail[v]=trie[ff][i],++in[trie[ff][i]];//注意更新入度
Q.push(v);
}
}
}
inline void Compare(const char* ss){
for(int i=0,siz=strlen(ss),p=1;i<siz;++i){
p=trie[p][ss[i]-'a'];
++tot[p];
}
}
inline void Runtopu(){
queue<int>Q;
rep(i,1,ncnt)if(!in[i])Q.push(i);
while(!Q.empty()){
int u=Q.front();Q.pop();
tot[fail[u]]+=tot[u];
--in[fail[u]];
if(in[fail[u]]==0)Q.push(fail[u]);
}
}
int n;
signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
qread(n);
rep(i,1,n){
scanf("%s",S);
Insert(S,i);
}
Getfail();
scanf("%s",T);
Compare(T);
Runtopu();
rep(i,1,n)writc(tot[enz[i]],'\n');
return 0;
}
「模板」AC自动机的更多相关文章
- LG5357 「模板」AC自动机(二次加强版) AC自动机+fail树
问题描述 LG5357 题解 不是fail树的AC自动机复杂度是假的. 把AC自动机搞出来,建立Trie树,树上爆搜一遍就好了. \(\mathrm{Code}\) #include<bits/ ...
- 「模板」AC自动机(ACAM)
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> u ...
- 「笔记」AC 自动机
目录 写在前面 定义 引入 构造 暴力 字典图优化 匹配 在线 离线 复杂度 完整代码 例题 P3796 [模板]AC 自动机(加强版) P3808 [模板]AC 自动机(简单版) 「JSOI2007 ...
- 「LuoguP3796」 【模板】AC自动机(加强版)
题目描述 有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据的第一 ...
- 洛谷P3796 - 【模板】AC自动机(加强版)
原题链接 Description 模板题啦~ Code //[模板]AC自动机(加强版) #include <cstdio> #include <cstring> int co ...
- P3796 【模板】AC自动机(加强版)
P3796 [模板]AC自动机(加强版) https://www.luogu.org/problemnew/show/P3796 题目描述 有NN个由小写字母组成的模式串以及一个文本串TT.每个模式串 ...
- 模板】AC自动机(简单版)
模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P3808 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保 ...
- 「模板」 线段树——区间乘 && 区间加 && 区间求和
「模板」 线段树--区间乘 && 区间加 && 区间求和 原来的代码太恶心了,重贴一遍. #include <cstdio> int n,m; long l ...
- 「模板」 FHQ_Treap 区间翻转
「模板」 FHQ_Treap 区间翻转 没有旋转的 Treap 实现区间操作的功能,很好理解,也很好写,只是速度不算太快. 对于要翻转的区间,把整棵 Treap(存有区间 \([1,n]\) 的信息) ...
随机推荐
- ECMAScript基本语法——⑤运算符 逻辑运算符
&&与,会短路:左边为false右边就不参与运算||或,会短路:左边为true右边就不参与运算!非, 注意:在JavaScript中,如果运算数不是运算符要求的类型,那么JavaScr ...
- 题解【洛谷P1083】[NOIP2012]借教室
题面 二分到哪一个申请人要修改订单,可以差分\(+\)前缀和达到\(\Theta(n)\)的\(\text{check}\). 具体细节见代码. #include <bits/stdc++.h& ...
- linux - mysql:安装mysql
安装环境 系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...
- Python目录结构规范
在设计大型项目时需要规范目录结构. 假设你的项目名为foo, 我比较建议的最方便快捷目录结构这样就足够了: Foo/ |-- bin/ | |-- foo | |-- foo/ | |-- tests ...
- spring aop使用,spring aop注解,Spring切面编程
================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...
- blob - 二进制文件流下载
/** * 返回值文件类型为 blob 二进制流文件 * responseType: 'blob' * params 接口所需参数 * 命名文件名:依据时间戳命名文件名 * (导出时需要延迟,否则导出 ...
- 简单的Spring1.0小配置
开始Spring AOP的小理解 拿一个小例子来说吧! 老师上课 这样的例子! 老师上课--就是一个核心的业务! 那么上课之前需要点名,天气太热,需要开空调! 这个时候,一个老 ...
- 题解【洛谷P3478】[POI2008]STA-Station
题面 设\(dp_i\)表示以\(i\)为根节点时所有节点的深度之和. 首先以 \(1\) 为根求出所有点深度之和\(dp_1\),并预处理每个点的子树大小. 设 \(v\) 是 \(u\) 的孩子, ...
- Spring - Spring Boot - Thymeleaf - textual 模式
概述 thymeleaf 的 text 模式简单使用 过程会比较啰嗦, 需要结论的同学, 可以直接到底部去寻找 背景 想尝试做一个简单的 模板工具 目的 自动生成一些简单的 重复文本 思路 尽量简单 ...
- CentOS7.0安装EMQ代理服务
CentOS7.0安装EMQ代理服务 安装文件下载 官网 : https://www.emqx.io/cn/ 下载地址: https://www.emqx.io/cn/downloads 选择需要的版 ...