[BZOJ3998][TJOI2015]弦论 后缀自动机

建出SAM。
假设T=0,那么除了根节点每个点代表1个串。
假设T=1,那么除了根节点每个点代表其right集合大小个字串。
先把每个点代表多少个串统计出来,然后就可统计统计转移到某个点之后还能走出多少个字串。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int maxn=1000010; 
int T,k;
char s[maxn];
struct sam
{
    int cnt,last,a[maxn][26],fa[maxn],mx[maxn],ri[maxn],buc[maxn],ord[maxn];
    ll sz[maxn];
    sam(){cnt=last=1;}
    void extend(int c)
    {
        int p=last,np=last=++cnt;mx[np]=mx[p]+1;ri[np]=1;
        while(p&&!a[p][c]) a[p][c]=np,p=fa[p];
        if(!p) fa[np]=1;
        else
        {
            int q=a[p][c];
            if(mx[q]==mx[p]+1) fa[np]=q;
            else
            {
                int nq=++cnt;mx[nq]=mx[p]+1;
                for(int ic=0;ic<=25;ic++)
                    a[nq][ic]=a[q][ic];
                fa[nq]=fa[q];
                fa[q]=fa[np]=nq;
                while(p&&a[p][c]==q) a[p][c]=nq,p=fa[p];
            }   
        }
    }
    void pre()
    {
        for(int i=1;i<=cnt;i++) buc[mx[i]]++;
        for(int i=1;i<=cnt;i++) buc[i]+=buc[i-1];
        for(int i=1;i<=cnt;i++) ord[buc[mx[i]]--]=i;
        for(int i=cnt;i>=1;i--) 
        {
            int t=ord[i];
            if(T==1) ri[fa[t]]+=ri[t];
            else ri[t]=1;
        }
        ri[1]=0;
        for(int i=cnt;i>=1;i--)
        {
            int t=ord[i];
            sz[t]=ri[t];
            for(int c=0;c<=25;c++)
                sz[t]+=sz[a[t][c]];
        }
    }
    void solve()
    {
        if(sz[1]<k) {puts("-1");return;}
        for(int p=1;k>0;k-=ri[p])
            for(int c=0;c<=25;c++)   
                if(sz[a[p][c]]<k) k-=sz[a[p][c]];
                else
                {
                    printf("%c",c+'a');
                    p=a[p][c];
                    break;
                }
    }
}Tsam;
int main()
{
    scanf("%s%d%d",s+1,&T,&k);
    int len=strlen(s+1);
    for(int i=1;i<=len;i++)
        Tsam.extend(s[i]-'a');
    Tsam.pre();
    Tsam.solve();    
    return 0;   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值