Sequence
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7923   Accepted: 1801
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 = 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

题意:给一个数列,将其分三段,使得每段分别反转组合后的字典序最小。
思路:P381
代码:
 //#include"bits/stdc++.h"
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"ctime"
#include"iostream"
#include"cstdlib"
#include"algorithm"
#define db double
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 2e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
int sa[N],rev[N];
int r[N];
int tmp[N];
int a[N];
int n,k;
int p1,p2;
bool cmp(int i,int j){
if(r[i] != r[j]) return r[i]<r[j];
else
{
int ri=i+k<=n?r[i+k]:-;
int rj=j+k<=n?r[j+k]:-;
return ri<rj;
}
}
void bulid(int s[N],int l,int *sa)
{ for(int i=;i<=l;i++){
sa[i]=i;
r[i]=i<l?s[i]:-;
}
for(k=;k<=l;k*=){
sort(sa,sa+l+,cmp);
tmp[sa[]]=;
for(int i=;i<=l;i++){
tmp[sa[i]]=tmp[sa[i-]]+(cmp(sa[i-],sa[i])?:);
}
for(int i=;i<=l;i++){
r[i]=tmp[i];
}
}
}
void cal()
{
memset(rev,, sizeof(rev));
memset(r,, sizeof(r));
memset(sa,, sizeof(sa));
reverse_copy(a,a+n,rev);
bulid(rev,n,sa);
for(int i=;i<=n;i++){
p1=n-sa[i];
if(p1>=&&n-p1>=){
break;
}
}
int m=n-p1;
reverse_copy(a+p1,a+n,rev);
reverse_copy(a+p1,a+n,rev+m);
bulid(rev,*m,sa);
for(int i=;i<=*m;i++){
p2=p1+m-sa[i];
if(p2-p1>=&&n-p2>=){
break;
}
}
reverse(a,a+p1);
reverse(a+p1,a+p2);
reverse(a+p2,a+n);
for(int i=;i<n;i++) printf("%d\n",a[i]);
}
int main()
{
ci(n);
for(int i=;i<n;i++) ci(a[i]);
cal();
return ;
}

POJ 3581 三段字符串(后缀数组)的更多相关文章

  1. POJ 3581 Sequence(后缀数组)题解

    题意: 已知某字符串\(str\)满足\(str_1 > max\{str_2,str_3 \cdots str_n\}\),现要求把这个字符串分成连续的三组,然后每组都翻转,问字典序最小是什么 ...

  2. POJ 3581 Sequence(后缀数组)

    [题目链接] http://poj.org/problem?id=3581 [题目大意] 给出一个数列,将这个数列分成三段,每段分别翻转,使得其字典序最小,输出翻转后的数列. [题解] 首先,第一个翻 ...

  3. poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串

    题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同, ...

  4. POJ 1226 Substrings(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=1226 [题目大意] 求在每个给出字符串中出现的最长子串的长度,字符串在出现的时候可以是倒置的. [题解] 我们将每个字符串倒置,用 ...

  5. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  6. Poj 3294 Life Forms (后缀数组 + 二分 + Hash)

    题目链接: Poj 3294 Life Forms 题目描述: 有n个文本串,问在一半以上的文本串出现过的最长连续子串? 解题思路: 可以把文本串用没有出现过的不同字符连起来,然后求新文本串的heig ...

  7. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  8. Bzoj4556: [Tjoi2016&Heoi2016]字符串 后缀数组

    4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 87[Sub ...

  9. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

随机推荐

  1. jq中事件绑定的方法

    在唯品会实习生面试中,被面试官问了这么一个问题,“jQuery中绑定事件的方法有几个?”,以click事件为例,我当时想到的只有.click(),.bind(),.on()这三种,然后面试官又追问,“ ...

  2. Linux yum apt-get 方式

    Linux 大致可以分两大类   RedHat分支 redhat, centos ,mandrake,mandriva,国产的红x等 1 常见的安装包格式 rpm包,安装rpm包的命令是“rpm -参 ...

  3. Android Studio 小技巧(1):如何导入AAR文件

    1. 导入AAR.JAR文件 File- > New -> New Module 这样子AAR文件就导入了,然后在app中的build.gradle中做如下添加 dependencies ...

  4. 轻松完成excel读写操作- 基于POI的框架BingExcel的使用(2)

    在进行 类转换时候,系统默认注册了一些转换器,当然你也可以自定局部或者全局的转换器.下面进行具体使用说明  java类说明 对应的java实体类,其属性必须要有__@CellConfig__注解,此注 ...

  5. 【Angular JS】网站使用社会化评论插件,以及过程中碰到的坑

    目前正在开发自己的网站,技术上使用Angular JS + Express JS + Mongo DB.由于网站会有文章发布,因此需要有评论功能.评论功能也可以自己开发,但由于现在社会化评论插件很多, ...

  6. 如何提升SharePoint 2010的性能

    文章来自: http://www.chinaemail.com.cn/server/xtfl/Exchange/201109/66466.html SharePoint是微软历史上销售量增长最快的产品 ...

  7. vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: guest_enable

    不然启动时会涌现毛病,举个例子 guest_enable=YES  后面出现空格,就会出现 为 vsftpd 启动 vsftpd:500 OOPS: bad bool value in config ...

  8. Spring boot 项目导出可执行jar

    配置文件中添加插件 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  9. xpath模块

    W3School官方文档:http://www.w3school.com.cn/xpath/index.asp 我们使用xpath主要是获取网页数据的,之前一直是使用bs4,xpath也是最近了解到的 ...

  10. Spring 框架配置web.xml 整合web struts

    package cn.itcast.e_web; import java.io.IOException; import javax.servlet.ServletContext; import jav ...