1. 程式人生 > >【BZOJ2882】工藝(後綴自動機)

【BZOJ2882】工藝(後綴自動機)

cond mes source ext space turn cto pre 構建

【BZOJ2882】工藝(後綴自動機)

題面

BZOJ權限題,良心洛谷

題解

還是一樣的,先把串在後面接一遍
然後構建\(SAM\)
直接按照字典序輸出\(n\)次就行了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std; #define ll long long #define RG register #define MAX 620000 inline int read() { RG int x=0,t=1;RG char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-')t=-1,ch=getchar(); while(ch<='9'&&ch>='0'
)x=x*10+ch-48,ch=getchar(); return x*t; } struct Node { map<int,int> son; int ff,len; }t[MAX<<1]; int last=1,tot=1; int n,a[MAX]; void extend(int c) { int p=last,np=++tot;last=np; t[np].len=t[p].len+1; while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff; if(!p)t[np].ff=1
; else { int q=t[p].son[c]; if(t[q].len==t[p].len+1)t[np].ff=q; else { int nq=++tot; t[nq]=t[q]; t[nq].len=t[p].len+1; t[q].ff=t[np].ff=nq; while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff; } } } int main() { n=read(); for(int i=1;i<=n;++i)a[i]=read(); for(int i=1;i<=n;++i)extend(a[i]); for(int i=1;i<=n;++i)extend(a[i]); for(int i=1,pos=1;i<=n;++i) { printf("%d ",t[pos].son.begin()->first); pos=t[pos].son.begin()->second; } puts(""); return 0; }

【BZOJ2882】工藝(後綴自動機)