跳到正文

题解

题解 P3966 【[TJOI2013]单词】

Mihari约 8 分钟
原文
文章信息与版本

文章信息

发布
2020年1月26日
收录
2026年10月24日
更新
2026年11月21日
篇幅
约 8 分钟
题目
P3966

版本

  1. v1当前

实在不知道为什么这是一道省选题,或许是 T1\sout{T1} 难度吧

一道 ACAC 自动鸡的板题。

模式串就是输入的那些字符串,用那些模式串建一只鸡即可。

而文本串可能要想一会——将模式串之间用一些特别的字符隔开,再拼起来,得到的最终的串就是我们的文本串。

至于剩下的,或许就是 ACAC 自动鸡的模板了。

如果有 ACAC 自动鸡不懂的,可以问度娘。

此处代码使用拓扑优化 ACAC 自动鸡,安利一位大佬关于 ACAC 自动鸡的讲解sto hyfhaha

C++
#include<cstdio>
#include<cstring>
#include<vector>
#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;
}

struct node{
    int u,fail;char cr;
    node(const int U=0,const int F=0,const char C='\0'):u(U),fail(F),cr(C){}
};

const int MAXK=27;
const int MAXN=200;
const int MAXSIZE=1e6+MAXN;
const char spc='z'+1;

int trie[MAXSIZE+5][MAXK+5],fail[MAXSIZE+5],enz[MAXN+5],N,ncnt=1;
int in[MAXSIZE+5],tot[MAXSIZE+5];

char T[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(){
    rep(i,0,MAXK)trie[0][i]=1;
    queue<int>Q;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,len=strlen(ss),p=1;i<len;++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]])Q.push(fail[u]);
    }
}

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    qread(N);
    for(int i=1,prelen=0;i<=N;++i){
        prelen=strlen(T);
        if(i!=1)T[prelen]=spc,++prelen;
        scanf("%s",T+prelen);
        Insert(T+prelen,i);
    }
    Getfail();
    Compare(T);
    Runtopu();
    rep(i,1,N)writc(tot[enz[i]],'\n');
    return 0;
}
原文
存档评论2 条抓取于