题面

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

相关文章:

  • 2022-03-05
  • 2021-10-02
  • 2021-08-16
  • 2021-07-26
  • 2021-12-27
  • 2022-12-23
  • 2022-03-07
  • 2022-01-13
猜你喜欢
  • 2021-08-13
  • 2021-12-09
  • 2021-10-16
  • 2021-11-26
  • 2021-08-19
  • 2021-06-13
  • 2022-02-10
相关资源
相似解决方案