POJ3581——Sequence(后缀数组)

白书上的例题,自己敲了一遍,也大概看懂了。


Sequence
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6452 Accepted: 1414
Case Time Limit: 2000MS

Description

Given a sequence, {A1A2, ..., An} which is guaranteed AA2, ..., 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 {A1A2, ..., An} and {B1B2, ..., Bn}, we say {A1A2, ..., An} is smaller than {B1B2, ..., Bn} if and only if there exists such i ( 1 ≤ i ≤ n) so that we have Ai < Bi and Aj = Bjfor 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}


题意:给你一段整数序列,现在要把这个序列分成三段,并将每段分别反转,求能得到的字典序最小的序列。要求每段得到的的序列都不为空,而且第一个数是最大的。

思路:首先因为第一个数是最大的,所以第一段很好确定,我们只要先把数组反转,再找到字典序最小的那个,并且保证后面的两个序列不为空。因为反转之后第一段最后一个肯定是a[0],也就是最大的数。后面的序列不会对第一段有影响。

然后是第二段和第三段。怎么能够转化一下,能够通过后缀数组得到结果呢?

我们把除了第一段的序列反转,再复制一次,接到一个数组的后面。

想想一下,我们从这个数组的前半部分找到一个点切割,当此时的后缀的字典序最小的时候,是不是也就是前面那两段序列分别反转后字典序最小?因为有两段重复的相接。


#include<cstdio>
#include<cstring>    
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 200010

int n,k;
int rank[MAXN+1];
int tmp[MAXN+1];

//比较(rank[i],rank[i+k])和(rank[j],rank[j+k])
bool compare_sa(int i,int j){
	if(rank[i]!=rank[j])
		return rank[i]<rank[j];
	else{
		int ri=i+k<=n?rank[i+k]:-1;
		int rj=j+k<=n?rank[j+k]:-1;
		return ri<rj;
	}
}
//rank用来记录字符串的排序,sa用来记录开头字符的位置,S用来记录字符串
//第一个通常是空字符串
void construct_sa(int *S,int n,int *sa){
	//初始长度为1,rank直接取字符的编码.
	for(int i=0;i<=n;i++){
		sa[i]=i;
		rank[i]=i<n?S[i]:-1;
	}

	//利用对长度为k的排序的结果对长度为2k的排序
	for(k=1;k<=n;k*=2){
		sort(sa,sa+n+1,compare_sa);

		//先在tmp中临时储存新计算的rank,再转存回rank中
		tmp[sa[0]]=0;
		for(int i=1;i<=n;i++){
			tmp[sa[i]]=tmp[sa[i-1]]+(compare_sa(sa[i-1],sa[i])?1:0);
		}
		for(int i=0;i<=n;i++){
			rank[i]=tmp[i];
		}
	}
}

int a[MAXN];
int rev[MAXN*2],sa[MAXN*2];
void solve(){
    //将a翻转,并计算其后缀数组
    reverse_copy(a,a+n,rev);
    construct_sa(rev,n,sa);

    //确定第一段的分隔位置
    int p1;
    //sa[i]是逐渐递增的,n-sa[i]是分割的长度,p1>=1代表至少在第一个位置,n-p1>=2代表不能让后面的两个数组为空
    for(int i=0;i<n;i++){
        p1=n-sa[i];
        if(p1>=1&&n-p1>=2)
            break;
    }

    //将p1之后的字符串反转并重复2次,再计算其后缀数组。
    int m=n-p1;
    reverse_copy(a+p1,a+n,rev);
    reverse_copy(a+p1,a+n,rev+m);
    construct_sa(rev,2*m,sa);

    //确定分隔位置
    int p2;
    for(int i=0;i<=2*m;i++){
        p2=p1+m-sa[i];
        if(p2-p1>=1&&n-p2>=1)
            break;
    }

    reverse(a,a+p1);
    reverse(a+p1,a+p2);
    reverse(a+p2,a+n);
    for(int i=0;i<n;i++){
        printf("%d\n",a[i]);
    }
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",a+i);
    solve();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值