POJ 3581 Sequence(后缀数组)
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
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,k,pos,a[],b[],c[],sa[],rk[],tmp[]; int cmp(int i,int j)
{
if(rk[i]!=rk[j]) return rk[i]<rk[j];
int ri=i+k<=n?rk[i+k]:-;
int rj=j+k<=n?rk[j+k]:-;
return ri<rj;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[n-i]=a[i];
}
for(int i=;i<n;i++)
{
sa[i]=i;
rk[i]=b[i];
}
sa[n]=n;
rk[n]=-;
for(k=;k<=n;k<<=)
{
sort(sa,sa+n+,cmp);
tmp[sa[]]=;
for(int i=;i<=n;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+cmp(sa[i-],sa[i]);
}
for(int i=;i<=n;i++)
{
rk[i]=tmp[i];
}
}
sort(sa,sa+n+,cmp);
for(int i=;i<=n;i++)
{
if(sa[i]!=&&sa[i]!=&&sa[i]!=n)
{
pos=sa[i];
break;
}
}
int len=n-pos;
int cnt=;
for(int i=n;i>=len+;i--)
{
c[cnt++]=a[i];
}
for(int i=cnt;i<cnt*;i++)
{
c[i]=c[i-cnt];
}
n=cnt*;
for(int i=;i<n;i++)
{
sa[i]=i;
rk[i]=c[i];
}
sa[n]=n;
rk[n]=-;
for(k=;k<=n;k<<=)
{
sort(sa,sa+n+,cmp);
tmp[sa[]]=;
for(int i=;i<=n;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+cmp(sa[i-],sa[i]);
}
for(int i=;i<=n;i++)
{
rk[i]=tmp[i];
}
}
for(int i=len;i>=;i--)
{
printf("%d\n",a[i]);
}
sort(sa,sa+n+,cmp);
for(int i=;i<=n;i++)
{
if(sa[i]+n/<n&&sa[i]!=)
{
for(int j=sa[i];j<=sa[i]+n/-;j++)
{
printf("%d\n",c[j]);
}
break;
}
}
}
POJ 3581 Sequence(后缀数组)的更多相关文章
- POJ 3581 Sequence ——后缀数组 最小表示法
[题目分析] 一见到题目,就有了一个显而易见obviously的想法.只需要每次找到倒过来最小的那一个字符串翻转就可以了. 然而事情并不是这样的,比如说505023这样一个字符串,如果翻转了成为320 ...
- POJ 3581 Sequence [后缀数组]
Sequence Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6911 Accepted: 1543 Case Tim ...
- [POJ 3581]Sequence
[POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...
- 后缀数组 POJ 3581 Sequence
题目链接 题意:把n个数字(A1比其他数字都大)的序列分成三段,每段分别反转,问字典序最小的序列. 分析:因为A1比其他数字都大,所以反转后第一段结尾是很大的数,相当是天然的分割线,第一段可以单独考虑 ...
- POJ 3581 Sequence(后缀数组)题解
题意: 已知某字符串\(str\)满足\(str_1 > max\{str_2,str_3 \cdots str_n\}\),现要求把这个字符串分成连续的三组,然后每组都翻转,问字典序最小是什么 ...
- POJ3581 Sequence —— 后缀数组
题目链接:https://vjudge.net/problem/POJ-3581 Sequence Time Limit: 5000MS Memory Limit: 65536K Total Su ...
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- POJ 1743-POJ - 3261~后缀数组关于最长字串问题
POJ 1743 题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1~~88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需 ...
- POJ - 1226 Substrings (后缀数组)
传送门:POJ - 1226 这个题跟POJ - 3294 和POJ - 3450 都是一样的思路,一种题型. POJ - 3294的题解可以见:https://www.cnblogs.com/li ...
随机推荐
- PHP对全局变量_GET、_POST中特殊字符的过滤
$sVariablesOrder = ini_get( 'variables_order' ); $request = array(); //过滤不安全数据 for ( $i ...
- 二、jenkins配置email(以腾讯企业qq为例)
废话不多说,直接上干货: 主要针对两个部分进行介绍: 1.jenkins内置的邮件功能: 2.Editable Email Notification插件的邮件功能: 低版本的jenkins有很多插件都 ...
- Error: listen EACCES 0.0.0.0:8080 错误解决记录
live-server -- 热加载利器 实现本地服务器,可及时刷新. 1.通过npm install -g live-server进行安装 2.npm init 初始化项目3.在所需要的文件夹内运行 ...
- LCS(最长公共子序列)动规算法正确性证明
今天在看代码源文件求diff的原理的时候看到了LCS算法.这个算法应该不陌生,动规的经典算法.具体算法做啥了我就不说了,不知道的可以直接看<算法导论>动态规划那一章.既然看到了就想回忆下, ...
- [Z]shell变量详解
原文:http://www.cnblogs.com/barrychiao/archive/2012/10/22/2733210.html 1 shell变量基础shell变量是一种很“弱”的变量,默认 ...
- [转] C# 获取程序运行目录
来自 莫等闲也,原文 // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnosti ...
- jaegeropentracing的Java-client完整分布式追踪链
jaegeropentracing的Java-client完整分布式追踪链,在分布式系统中透传trace信息 之前文章记录了jaegeropentracing的Java-client追踪链在单系统中的 ...
- Struts2 配置Action详解
Struts2的核心功能是action,对于开发人员来说,使用Struts2主要就是编写action,action类通常都要实现com.opensymphony.xwork2.Action接口,并实 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- [iOS]swift版内购
//内购Demo,看代码说话吧 class IAPTestViewController: UIViewController ,SKProductsRequestDelegate, SKPaymentT ...