Codeforces 235 C(后缀自动机)

传送门

题意:

给你一个主串 s t r str str以及 n n n个模式串。问你这 n n n个模式串的所有循环同构在主串 s t r str str中的出现次数。

题目分析:

这是一个需要转换思维的题目。

首先,如果题目中不要求我们求每一个模式串的循环同构的出现此时,显然这道题我们直接可以用后缀自动机进行求解。而现在的问题就是我们应当如何在比较优的时间内将一个字符串中的所有循环同构的出现次数。

我们首先对主串建立后缀自动机。

其次,我们考虑将模式串 s t r str str复制并接在原串后面,形成一个新串 s t r s t r strstr strstr,这样,新串中的每一个长度等于 ∣ s t r ∣ |str| str的子串都可以表示为串 s t r str str的其中一个循环同构子串。此时,我们发现,如果我们用新串 s t r s t r strstr strstr在后缀自动机上匹配,匹配到的每个长度为 ∣ s t r ∣ |str| str的子串出现次数之和即为答案。因此,在我们在后缀自动机上对 s t r s t r strstr strstr串进行匹配的过程中,我们只需要记录一下当前成功匹配的长度。倘若当前成功匹配的长度大于 ∣ s t r ∣ |str| str,则我们只需要在 p a r e n t parent parent树上跳转,直到出现一个状态 s t i st_i sti,使得 l e n [ s t i ] + 1 ≤ ∣ s t r ∣ len[st_i]+1\le|str| len[sti]+1str,此时我们只需要把该状态 s t i st_i sti所对应的所有子串的出现次数 ∣ e n d p o s ( s t i ) ∣ |endpos(st_i)| endpos(sti)累加到答案即可。

而某状态 s t i st_i sti所对应的所有子串的出现次数 ∣ e n d p o s ( s t i ) ∣ |endpos(st_i)| endpos(sti),我们可以用拓扑排序在建立自动机的过程中预处理出来。

PS:后缀自动机要开双倍/四倍空间,否则会疯狂RE

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
char str[maxn];
int n,ans[maxn];
struct SAM{
    int next[maxn*2][26],fa[maxn*2],l[maxn*2];
    int last,cnt=0;
    int cntA[maxn*2],A[maxn*2];
    int num[maxn*2];
    SAM(){clear();}
    void clear(){
        last=cnt=1;
        fa[1]=l[1]=0;
        memset(next[1],0,sizeof(next[1]));
    }
    void init(char *s){
        while(*s){
            Insert(*s-'a');
            s++;
        }
    }
    void Insert(int c){
        int p=last;
        int np=++cnt;
        num[np]=1;
        memset(next[cnt],0,sizeof(next[cnt]));
        l[np]=l[p]+1;
        last=np;
        while(p&&!next[p][c]) next[p][c]=np,p=fa[p];
        if(!p) fa[np]=1;
        else{
            int q=next[p][c];
            if(l[q]==l[p]+1) fa[np]=q;
            else{
                int nq=++cnt;
                l[nq]=l[p]+1;
                memcpy(next[nq],next[q],sizeof next[nq]);
                fa[nq]=fa[q];
                while(next[p][c]==q) next[p][c]=nq,p=fa[p];
                fa[np]=fa[q]=nq;
            }
        }
    }
    void build(char *s){
        memset(cntA,0,sizeof(cntA));
        memset(A,0,sizeof(A));
        int len=strlen(s);
        for(int i=1;i<=cnt;i++) cntA[l[i]]++;
        for(int i=1;i<=cnt;i++) cntA[i]+=cntA[i-1];
        for(int i=cnt;i>=1;i--) A[cntA[l[i]]--] =i;
        int tmp=1;
        for(int i=cnt;i>=1;i--){
            int x=A[i];
            num[fa[x]]+=num[x];
        }
    }
    void query(int len,int id){
        int now=1,ans=0,L=0;
        int vis[maxn<<1];
        for(int i=1;i<=len+len;i++){
            int x=str[i]-'a';
            if(next[now][x]) L++,now=next[now][x];
            else{
                while(now&&!next[now][x]) now=fa[now];
                if(!now) now=1,L=0;
                else L=l[now]+1,now=next[now][x];
            }
            if(i>len&&L>=len){
                int tmp=now;
                while(tmp&&!(l[fa[tmp]]+1<=len&&l[tmp]>=len)) tmp=fa[tmp];
                if(!tmp) tmp=1;
                if(vis[tmp]!=id) ans+=num[tmp],vis[tmp]=id;
            }
        }
        printf("%d\n",ans);
    }
}sam;
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%s",str);
    sam.clear();
    sam.init(str);
    sam.build(str);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",str+1);
        int len=strlen(str+1);
        for(int i=1;i<=len;i++){
            str[i+len]=str[i];
        }
        sam.query(len,i);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值