显然看到题第一反应sam /sa瞎搞搞。。。
然后看到数据范围,发现值域过大…
其实也无所谓…套个map,多个log搞定。。。
当然其实可以用 最小表示法 (O(n)). (我不会orz)
c++代码如下:
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 3e5+50;
struct SAM
{
map<int,int> ch;
int len,pre;
}t[4*N];
int n,root = 1,sz = 1,lst = 1,np,a[N];
inline void insert(int x)
{
int np = ++ sz,p = lst;
t[np].len = t[p].len + 1;
for(;p && !t[p].ch[x];p = t[p].pre) t[p].ch[x] = np;
if(!p) t[np].pre = root;
else
{
int q = t[p].ch[x];
if(t[p].len + 1 == t[q].len) t[np].pre = q;
else
{
int nq = ++ sz;
t[nq] = t[q]; t[nq].len = t[p].len + 1;
t[np].pre = t[q].pre = nq;
for(;p && t[p].ch[x] == q; p = t[p].pre) t[p].ch[x] = nq;
}
}
lst = np;
}
int main()
{
read(n);
rep(i,1,n)
{
read(a[i]);
insert(a[i]);
}
rep(i,1,n) insert(a[i]);
int now = 1;
rep(i,1,n)
{
map<int,int>::iterator it;
it = t[now].ch.begin();
printf("%d ",(*it).first);
now = (*it).second;
}
return 0;
}