您的位置:首页 > 产品设计 > UI/UE

【poj3581】Sequence 后缀数组

2016-02-26 18:41 363 查看

Description

Given a sequence, {A1, A2, …, An} which is guaranteed A1 > A2, …, An, you are to cut it into three sub-sequences and reverse them separately to form a new one which is the smallest possible sequence in alphabet order.

The alphabet order is defined as follows: for two sequence {A1, A2, …, An} and {B1, B2, …, Bn}, we say {A1, A2, …, An} is smaller than {B1, B2, …, Bn} if and only if there exists such i ( 1 ≤ i ≤ n) so that we have Ai < Bi and Aj = Bj for each j < i.

Input

The first line contains n. (n ≤ 200000)

The following n lines contain the sequence.

Output

output n lines which is the smallest possible sequence obtained.

Sample Input

5
10
1
2
3
4


Sample Output

1
10
2
4
3


Hint

{10, 1, 2, 3, 4} -> {10, 1 | 2 | 3, 4} -> {1, 10, 2, 4, 3}

Source

POJ Founder Monthly Contest – 2008.04.13, Yao Jinyu

把串分成三份,把每份翻转,使得最终串的字典序最小。三份不能有空串。

这个后缀数组好蛋疼…代码恶心,最后main函数我抄的书233

因为第一个元素最大,所以第一段很好找,找反串的最小字典序的后缀即可。

然后剩下就不能这么简单地分了,可以考虑:

若把后面分为两段,然后把它们翻转,使得字典序最小,相当于把串复制一份接后面然后求反串的最小字典序后缀。

例如串是这样:ABCD

扩大一倍成这样:ABCDABCD

反串:DCBADCBA

若解是把AB和CD翻转,那么BADC的字典序最小,对应的在反串里的后缀就是BADCBA,取前缀即可。

要注意合法,要注意细节

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int SZ = 1000010;

int n;

int sa[SZ],rank[SZ],k = 1,tmp[SZ];

bool cmp_sa(int i,int j)
{
if(rank[i] != rank[j]) return rank[i] < rank[j];
else
{
int x = i + k <= n ? rank[i + k] : -1;
int y = j + k <= n ? rank[j + k] : -1;
return x < y;
}
}

void get_sa(int s[],int n)
{
for(int i = 0;i <= n;i ++)
{
sa[i] = i;
rank[i] = i == n ? -1 : s[i];
}
for(k = 1;k <= n;k <<= 1)
{
sort(sa,sa + 1 + n,cmp_sa);

tmp[sa[0]] = 0;
for(int i = 1;i <= n;i ++)
tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0);
for(int i = 0;i <= n;i ++)
rank[i] = tmp[i];
}
}

int num[SZ],s[SZ];
int tmp2[SZ];

void copy(int num[],int l,int r,int s[])
{
for(int i = l;i < r;i ++)
s[i - l] = num[i];
}
void rev(int s[],int l,int r)
{
for(int i = l;i < r;i ++)
{
tmp2[i] = s[r - i - 1 + l];
//      cout<<tmp2[i]<<" "<<i<<endl;
}
//  puts("");
for(int i = l;i < r;i ++)
s[i] = tmp2[i];
//  for(int i = 0;i < n;i ++)   printf("%d ",num[i]); puts("");
}

int main()
{
scanf("%d",&n);
for(int i = 0;i < n;i ++)
scanf("%d",&num[i]);

copy(num,0,n,s); rev(s,0,n);

get_sa(s,n);

int p1;
for(int i = 0;i < n;i ++)
{
p1 = n - sa[i];
if(p1 >= 1 && n - p1 >= 2) break;
}

int m = n - p1;
copy(num,p1,n,s);
copy(num,p1,n,s + m);
rev(s,0,m * 2);

get_sa(s,m * 2);

int p2;
for(int i = 0;i <= m * 2;i ++)
{
p2 = p1 + m - sa[i];
if(p2 - p1 >= 1 && n - p2 >= 1) break;
}
//  for(int i = 0;i < n;i ++)   printf("%d ",num[i]);
//  cout<<endl<<p1<<" 233333 "<<p2<<endl;
rev(num,0,p1); rev(num,p1,p2); rev(num,p2,n);
for(int i = 0;i < n;i ++)   printf("%d\n",num[i]);

return 0;
}
/*
2 3 4 2 3 4

4 3 2 4 3 2
0 1 2 3 4 5

8
5 0 3 1 2 3 1 4

4 1 3 2 1 3 0 5

3 1 2 3 1 4 3 1 2 3 1 4

4 1 3 2 1 3 4 1 3 2 1 3

5
5 4 3 2 1

1 2 1 2

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: