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. Spring课程 Spring入门篇 1-1Spring入门课程简介

    课程链接: 课程简介: 1 什么是框架 2 Spring简介 3 IOC(配置,注解) 4 Bean(配置,注解) 5 AOP(配置,注解,AspectJ.API) SpringFrameWork 常 ...

  2. 简单封装的web里面的tab点击和swipe滑动的小插件

    简单封装的一个web的手势,tab和swipe,里面的具体数值都是自定义上去的,没有实际的标准,可以自行去修改都行 前两个是详解,js插件在最后一部分代码 ``` //封装web的tab步骤详解 &l ...

  3. IEnumerable<T> 用法

    //以下参考来自 http://www.cnblogs.com/wilber2013/p/4299529.html

  4. 如何找到Android app启动activity和页面元素信息

    在实施app自动化的时候,我们需要知道app 的启动activity和页面元素信息,以此启动app和定位页面元素,那么如何在没有源码的情况下找打他们呢?当然是有好的工具啦,有Android sdk自带 ...

  5. SSM事务

    问题描述:查询用户信息时想级联查出用户订单以及订单详情,在查询用户的时候JDBC是will be managed by Spring,但懒加载用户订单以及订单详情时就will not be manag ...

  6. Python基础学习之序列(2)

    通用序列操作 所有序列类型都可以进行某些特定的操作.这些操作包括:索引(indexing).分片(sliceing).加(adding).乖(multiplying)以及检查某个元素是否属于序列的成员 ...

  7. PHP : 数据库中int类型保存时间并通过年月份时分秒进行显示

    1.表设计: 2.数据库操作页面:将时间戳插入到数据库中 我们到数据库中可以看到: 3.我们将数据进行显示: 页面结果:(二维数组) 4.以为用mysqli_fetch_all得到的是二维数组,那么我 ...

  8. 第二章 LCD液晶显示屏&声控装置&播放音乐&遥控器

    这节我将带大家了解亮宁机器人编程的基础部分. LCD液晶显示屏 LCD液晶显示屏是在实现某种功能和调试中不可缺少的部分,接下来我带大家学习,如何使用LCD液晶显示屏. 首先我们把LCD液晶显示屏插入主 ...

  9. 支持多域名的免费SSL证书

    知乎网友称其支持多域名: https://www.zhihu.com/question/19578422 配置教程: https://www.cnblogs.com/duanweishi/p/8483 ...

  10. SVN建立分支、代码合并以及常用操作

    在项目开发的过程中,现在遇到这样一个问题: 现在是9月份,在同一个项目中我要开发A.B两个模块,A模块是11月份上线,B模块是12月份上线,但是SVN上的trunk(主干)上的代码必须是上线的. 假设 ...