洛谷P3975【天津省选2015】(后缀自动机DP)

5 篇文章 0 订阅
2 篇文章 0 订阅

题目链接

https://www.luogu.com.cn/problem/P3975

题解

此题非常经典且重要,是sam的函谷关,必须拿下。

  • 记录每个点endpos大小的方法是在parent树从下往上递推。开始的is_pre就是看看当前点与没有包含原串前缀,有的话,这个会在parent tree分叉时候丢掉。也就是当前节点并不是所有子节点的endpos的并集,并集还得加上那个前缀的结束位置才是当前点的endpos集合。
  • 求endpos大小的dp非常经典。
  • dp数组表示从某个点出发沿自动机往下走的子串有多少。每个点一部分子串是出边的字符加上自动机下一个状态的字符串形成的串(dp[nex]),一部分是边字符自己形成的串(endpos[nex])。(以上dp是算相同的)。
  • 之后像搜索树一样在自动机上搜索。注意往下走的时候也会消耗一定数量的串。
  • 天津oi的风格就是数据结构模板题。看看代码就懂了。

AC代码

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int NN=5e5+10;
struct NODE
{
    int ch[26];
    int len,fa;
    NODE(){memset(ch,0,sizeof(ch));len=0;}
}dian[NN<<1];
int is_pre[NN<<1];//标记是否是前缀
int las=1,tot=1;
void add(int c)
{
    int p=las;int np=las=++tot;
    is_pre[np]=1;//endpos[np]={i} where s[i]=c
    dian[np].len=dian[p].len+1;
    for(;p&&!dian[p].ch[c];p=dian[p].fa)dian[p].ch[c]=np;
    if(!p){dian[np].fa=1;}//以上为case 1
    else
    {
        int q=dian[p].ch[c];
        if(dian[q].len==dian[p].len+1)dian[np].fa=q;//以上为case 2
        else
        {
            int nq=++tot;dian[nq]=dian[q];//endpos不是i了
            dian[nq].len=dian[p].len+1;
            dian[q].fa=dian[np].fa=nq; 
            for(;p&&dian[p].ch[c]==q;p=dian[p].fa)dian[p].ch[c]=nq;//以上为case 3
        }
    }
}
int n;
vector<int>parentcon[NN<<1];
char s[NN];
void sam(){
    las=1,tot=1;
    for(int i=1;i<(n<<1);i++)is_pre[i]=0;
    for(int i=1;i<=n;i++)add(s[i]-'a');
    for(int i=1;i<=tot;i++){
        parentcon[dian[i].fa].push_back(i);
    }
}
long long endpos[NN<<1],dp[NN<<1];
void get_endpos(int cur,int fa){
    int up=parentcon[cur].size();
    endpos[cur]=is_pre[cur];
    for(int i=0;i<up;i++){
        int nex=parentcon[cur][i];
        if(nex!=fa){
            get_endpos(nex,fa);
            endpos[cur]+=endpos[nex];
        }
    }
}
void get_num(int cur){//算相同
    if(dp[cur])return;
    dp[cur]=0;
    for(int i=0;i<26;i++){
        int nex=dian[cur].ch[i];
        if(nex){
            get_num(nex);
            dp[cur]+=dp[nex]+endpos[nex];//dp[nex]是i+xxx的数量,endpos[nex]是i的数量,dp不含空串
        }
    }
}
void get_num_dif(int cur){//不算相同
    if(dp[cur])return;
    dp[cur]=1;//含空串
    for(int i=0;i<26;i++){
        int nex=dian[cur].ch[i];
        if(nex){
            get_num_dif(nex);
            dp[cur]+=dp[nex];
        }
    }
}
int ans[NN],anslen;
int t;
int dfs(int cur,int k,int deep){
    if(k<=0){anslen=deep-1;return k;}
    for(int i=0;i<26;i++){
        int nex=dian[cur].ch[i];
        if(nex){
            int simp=(t?endpos[nex]:1);
            if(dp[nex]+simp>=k){
                ans[deep]=i;
                return dfs(nex,k-simp,deep+1);
                break;
            }
            else{
                k-=(dp[nex]+simp);
            }
        }
    }
    return k;
}

int len;
int main()
{

    scanf("%s",s+1);n=strlen(s+1);
    sam();
    int k;
    scanf("%d%d",&t,&k);
    memset(dp,0,sizeof(dp));
    memset(endpos,0,sizeof(endpos));
    if(t==1){
        get_endpos(1,0);
        get_num(1);
        if(k>dp[1])printf("-1\n");
        else{
            dfs(1,k,1);
            for(int i=1;i<=anslen;i++)printf("%c",ans[i]+'a');
            printf("\n");
        }
    }
    else{
        get_num_dif(1);
        for(int i=1;i<=tot;i++)dp[i]--;//去空串
        if(k>dp[1])printf("-1\n");
        else{
            dfs(1,k,1);
            for(int i=1;i<=anslen;i++)printf("%c",ans[i]+'a');
            printf("\n");
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值