【BZOJ2882】工艺(后缀自动机)

【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;
}

posted @ 2018-02-21 20:22  小蒟蒻yyb  阅读(294)  评论(0编辑  收藏  举报