POJ3581:Sequence(后缀数组)

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

题意:把数组分成3分,每一份都翻转,要求输出字典序最小的结果

思路:参照http://blog.163.com/just_gogo/blog/static/1914390652011823103842787/

第二次求之所以要复制一次我们可以这样理解

首先320505,单纯这个的话,后缀按字典序排,第一是05,第二是0505

但是复制了之后,是第一是0505320505,第二是05320505

可见复制一遍还是有必要的

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 2000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
int wa[N],wb[N],wsf[N],wv[N],sa[N];
int rank[N],height[N],s[N];
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int cmp(int *r,int a,int b,int k)
{
    return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)//n要包含末尾添加的0
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++)  wsf[i]=0;
    for(i=0; i<n; i++)  wsf[x[i]=r[i]]++;
    for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
    for(i=n-1; i>=0; i--)  sa[--wsf[x[i]]]=i;
    p=1;
    j=1;
    for(; p<n; j*=2,m=p)
    {
        for(p=0,i=n-j; i<n; i++)  y[p++]=i;
        for(i=0; i<n; i++)  if(sa[i]>=j)  y[p++]=sa[i]-j;
        for(i=0; i<n; i++)  wv[i]=x[y[i]];
        for(i=0; i<m; i++)  wsf[i]=0;
        for(i=0; i<n; i++)  wsf[wv[i]]++;
        for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
        for(i=n-1; i>=0; i--)  sa[--wsf[wv[i]]]=y[i];
        t=x;
        x=y;
        y=t;
        x[sa[0]]=0;
        for(p=1,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
    }
}

struct node
{
    int id,num;
} a[N];

int cmp1(node a,node b)
{
    if(a.num!=b.num)
        return a.num<b.num;
    return a.id<b.id;
}

int main()
{
    int n,i,j,k,maxn;
    scanf("%d",&n);
    for(i = n-1; i>=0; i--)
    {
        scanf("%d",&a[i].num);
        a[i].id = i;
    }
    sort(a,a+n,cmp1);
    for(i = 0; i<n; i++)
    {
        if(i && a[i].num == a[i-1].num)
        {
            s[a[i].id] = s[a[i-1].id];
            continue;
        }
        s[a[i].id] = i+1;
    }
    s[n] = 0;
    getsa(s,sa,n+1,n+10);
    for(i = 1; i<=n&&sa[i]<=1; i++);

    int tem = sa[i];
    for(i = tem; i<n; i++)
        printf("%d\n",a[s[i]-1].num);
    for(i = 0; i<tem; i++)
        s[i+tem] = s[i];
    tem*=2;
    s[tem] = 0;
    getsa(s,sa,tem+1,n+10);
    int p;
    for(i = 1; i<tem&&(!sa[i]||sa[i]>=tem/2); i++) ;
    p = sa[i];
    for(i = p; i<tem/2; i++)
        printf("%d\n",a[s[i]-1].num);
    for(i = 0; i<p; i++)
        printf("%d\n",a[s[i]-1].num);

    return 0;
}
时间: 2024-10-10 05:17:07

POJ3581:Sequence(后缀数组)的相关文章

POJ 题目3581 Sequence(后缀数组)

Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5738   Accepted: 1227 Case Time Limit: 2000MS Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to cut it into three sub-sequences and

POJ3581 Sequence(后缀数组)

题意:给一个串,串的第一个字符比后面的都大,要把它分成三段,然后反转每一段,求能得到的字典序最小的串是什么. 首先,第一段是可以确定的:把原串反转,因为第一个字符是最大的,它是唯一的,不存在反转串的后缀之间有包含关系,所以取最小的后缀这就是第一段的字符串: 然后后面两段,如果确定分割位置可以发现这两段字符串构成是一个从分割位置出发逆时针循环回来的串—— 即接下来要求的就是剩余部分的反转的最小表示,可以用后缀数组来做:把串加长一倍,答案就在最小的且长度大于等于原串长度的后缀了. 注意,分的段要非空

(后缀数组)poj 3581 Sequence

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 defin

POJ 3581 Sequence (后缀数组+离散化)

Sequence 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

Sequence POJ - 3581 (后缀数组)

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 defin

后缀数组 POJ 3581 Sequence

题目链接 题意:把n个数字(A1比其他数字都大)的序列分成三段,每段分别反转,问字典序最小的序列. 分析:因为A1比其他数字都大,所以反转后第一段结尾是很大的数,相当是天然的分割线,第一段可以单独考虑,即求整段的字典序最小的后缀.后面两段不能分开考虑, 例子: 98 4 -1 5 0 5 0 2 3第一步:3 2 0 5 0 5 -1 4 8 对应输出 -1 4 8第二步3 2 0 5 0 5(开始的时候我并没有复制一遍) 对应输出:0 5第三步3 2 0 5    对应输出: 3 2 0 5可

poj 3581 Sequence(后缀数组,离散化)详解

题目链接:http://poj.org/problem?id=3581 题目大意:给一个数列,要求将其分成三段,每段进行翻转后形成后合并成新数列,求按字典顺序最小的新数列. 思路: 注意到题目中数列a0,a2,a3...an-1, a0是最大的,因此将原数列翻转后an-1,an-2,...,a1,a0,求后缀数组, sa[0]所代表的后缀即为所求第一段翻转后的数列,注意到要分成三份,因此sa[0]<2时不可取,此时找sa[1], sa[2]看是否可取.找第一个位置后,设剩下 数列是an-1,an

Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)

这题做得比较复杂..应该有更好的做法 题目大意: 有一个括号序列,可以对其进行两种操作: ·        向里面加一个括号,可以在开头,在结尾,在两个括号之间加. ·        对当前括号序列进行循环移动,即把最后一个括号拿到开头来. 上述两种操作可以做任意次,要求添加最少的括号使得原序列变成一个合法括号序列.如果有多种可能,输出字典序最小的那一个."(" < ")". 题解: 首先计算左括号和右括号的数量,可以知道,不妨假设左括号的数量大于右括号 那么

poj3581Sequence(后缀数组)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Sequence Time Limit: 5000MS   Memory Limit: 65536K Case Time Limit: 2000MS Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to cut it into three