POJ3581---Sequence 后缀树组

时间:2021-08-07 21:03:30

题意:n个数字组成的序列,第一个数字最大,,把序列分成3部分,每个部分分别翻转,输出翻转后字典序最小的序列。。

后缀数组变一下,,先求出 第一个分割的位置,,然后再求一次后缀数组,,求出第二个位置。。输出就好了。

此题要采用单组输入。。。

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 2e5+;
int s[maxn], rev_s[maxn << ];
int sa[maxn], rank[maxn], tmp[maxn];
int k, sort_len;
bool cmp(int i, int j)
{
if (rank[i] != rank[j])
return rank[i] < rank[j];
else
{
int x = i + k <= sort_len ? rank[i+k] : -;
int y = j + k <= sort_len ? rank[j+k] : -;
return x < y;
}
}
void build_sa(int str[], int len)
{
sort_len = len;
for (int i = ; i <= len; i++)
{
sa[i] = i;
rank[i] = i < len ? str[i] : -;
}
for (k = ; k <= len; k *= )
{
sort(sa, sa + len + , cmp);
tmp[sa[]] = ;
for (int i = ; i <= len; i++)
{
tmp[sa[i]] = tmp[sa[i-]] + (cmp(sa[i-],sa[i]) ? : );
}
for (int i = ; i <= len; i++)
rank[i] = tmp[i];
}
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
//while (~scanf ("%d", &n))
scanf ("%d", &n);
{
for (int i = ; i < n; i++)
scanf ("%d", s + i);
reverse_copy(s, s + n, rev_s);
build_sa(rev_s, n);
int pos1;
for (int i = ; i <= n; i++)
{
pos1 = n - sa[i] - ;
if (pos1 >= && pos1 <= n - )
break;
}
int len = n - pos1 - ;
reverse_copy(s+pos1+, s+n, rev_s);
reverse_copy(s+pos1+, s+n, rev_s+len);
build_sa(rev_s, len << );
int pos2;
for (int i = ; i <= * len; i++)
{
pos2 = len - sa[i] - ;
if (sa[i] < len && pos1++pos2 < n-)
break;
}
for (int i = pos1; i >= ; i--)
printf("%d\n",s[i]);
for (int i = pos2+pos1+; i > pos1; i--)
printf("%d\n",s[i]);
for (int i = n-; i > pos2+pos1+; i--)
printf("%d\n", s[i]);
}
return ;
}