「模板」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;
}
posted @ 2020-01-21 21:15  Arextre  阅读(116)  评论(0编辑  收藏  举报