洛谷 P1368 工艺 后缀自动机 求最小表示

洛谷 P1368 工艺 后缀自动机 求最小表示

后缀自动机沙茶题

将字符串复制一次,建立后缀自动机.

在后缀自动机上贪心走 $n$ 次即可.

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#define setIO(s) freopen(s".in","r",stdin) 
#define maxn 1000000 
using namespace std;
int n,arr[maxn],m,last=1,tot=1,len[maxn],f[maxn];  
map<int,int>ch[maxn]; 
map<int,int>::iterator  it;
void ins(int c){
    int p=last,np=last=++tot,q;
    len[np]=len[p]+1;
    while(p&&!ch[p][c]) ch[p][c]=np,p=f[p]; 
    if(!p) f[np]=1;
    else{
        q=ch[p][c];
        if(len[q]==len[p]+1)f[np]=q;
        else
        {
            int nq=++tot;
            len[nq]=len[p]+1;
            ch[nq]=ch[q];
            f[nq]=f[q];
            f[np]=f[q]=nq;
            while(p&&ch[p][c]==q)
                ch[p][c]=nq,p=f[p];
        }
    }
}
int main(){
    //setIO("input"); 
    scanf("%d",&n),m=2*n; 
    for(int i=1;i<=n;++i) scanf("%d",&arr[i]);
    for(int i=n+1;i<=m;++i) arr[i]=arr[i-n]; 
    for(int i=1;i<=m;++i) ins(arr[i]); 
    int p=1;
    while(n -- )
    {
        it=ch[p].begin();
        printf("%d" , it->first);
        if(n) printf(" ");
        p = it->second;
    }
    return 0; 
}

  

posted @ 2019-01-19 00:08 EM-LGH 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值