洛谷 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; 
}

  

原文地址:https://www.cnblogs.com/guangheli/p/10290257.html