「模板」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;
}

猜你喜欢

转载自www.cnblogs.com/Arextre/p/12225785.html