POJ3581: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 <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;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ3581:Sequence(后缀数组)的更多相关文章
- POJ3581 Sequence —— 后缀数组
题目链接:https://vjudge.net/problem/POJ-3581 Sequence Time Limit: 5000MS Memory Limit: 65536K Total Su ...
- POJ 3581 Sequence(后缀数组)
Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An, you are to ...
- POJ 3581 Sequence ——后缀数组 最小表示法
[题目分析] 一见到题目,就有了一个显而易见obviously的想法.只需要每次找到倒过来最小的那一个字符串翻转就可以了. 然而事情并不是这样的,比如说505023这样一个字符串,如果翻转了成为320 ...
- POJ 3581 Sequence [后缀数组]
Sequence Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6911 Accepted: 1543 Case Tim ...
- POJ3581 Sequence(后缀数组)
题意:给一个串,串的第一个字符比后面的都大,要把它分成三段,然后反转每一段,求能得到的字典序最小的串是什么. 首先,第一段是可以确定的:把原串反转,因为第一个字符是最大的,它是唯一的,不存在反转串的后 ...
- 后缀数组 POJ 3581 Sequence
题目链接 题意:把n个数字(A1比其他数字都大)的序列分成三段,每段分别反转,问字典序最小的序列. 分析:因为A1比其他数字都大,所以反转后第一段结尾是很大的数,相当是天然的分割线,第一段可以单独考虑 ...
- Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)
这题做得比较复杂..应该有更好的做法 题目大意: 有一个括号序列,可以对其进行两种操作: · 向里面加一个括号,可以在开头,在结尾,在两个括号之间加. · 对当前括号序列进 ...
- POJ 3581 Sequence(后缀数组)题解
题意: 已知某字符串\(str\)满足\(str_1 > max\{str_2,str_3 \cdots str_n\}\),现要求把这个字符串分成连续的三组,然后每组都翻转,问字典序最小是什么 ...
- [POJ3581]Sequence
[POJ3581]Sequence 题目大意: 给定序列\(A_{1\sim n}\),其中\(A_1\)为最大的数.要把这个序列分成\(3\)个非空段,并将每一段分别反转,求能得到的字典序最小的序列 ...
随机推荐
- Android利用Get、Post 获取网络数据
首先是Get: 布局非常easy.就一个button,主要看一下MainActivity吧: package com.francis.httpget; import android.app.Activ ...
- GitLal+sourceTree版本号管理
GitLab+sourceTree版本号管理 假设把代码提交到github上,仅仅能是公开的,除非你花钱了,可是我不想花钱,所以我选择了gitLab作为gitserver,使用gitLab的详细过程例 ...
- cocostudio内存释放
在使用cocostudio时,在释放内存时能够这样做: 在onExit()方法里加入例如以下: void LoadLayer::onExit() { // 释放本对象自己 removeFromPare ...
- Java8高中并发
Java8中学并发 本文翻译自:http://jaxenter.com/lean-concurrency-in-java-8-49924.html 转载请注明出处:http://blog.csdn.n ...
- 使用异步HTTP提升客户端性能(HttpAsyncClient)
使用异步HTTP提升客户端性能(HttpAsyncClient) 大家都知道,应用层的网络模型有同步.异步之分. 同步,意为着线程阻塞,只有等本次请求全部都完成了,才能进行下一次请求. 异步,好处是不 ...
- hdu4857 逃生 bestcoder round1 A
题目要求要求在满足约束条件的情况下,使小的序号尽力靠前. 坑点就在这里.小的序号尽量靠前并非代表字典序,它要求多种情况时,先使1靠前(可能1仅仅能在第2或第3位 那么就要使它在第2位),其次2,3. ...
- C#实现仿QQ震动
前提:新建winForm窗体应用程序,放置一个Button,设置按钮的单击事件 ; i < ; i++) { Point p = this.FindForm().Location; ,p.Y+) ...
- css 简单 返回顶部 代码及注释说明
1. 最简单的静态返回顶部,点击直接跳转页面顶部,常见于固定放置在页面底部返回顶部功能 方法一:用命名锚点击返回到顶部预设的id为top的元素 html代码 <a href="#top ...
- cocos2d-html5游戏图片资源选择
cocos2d-html5游戏图片资源能够选择,单张的图片作为一个精灵或者场景的载入对象.也能够把图片给做成plist文件.通过plist来訪问图片资源.其中优缺点.使用方式在个人的測试其中体现例如以 ...
- mongodb之java CRUD 简单操作
我下载的是 mongo-2.8.0.jar — Version 2.8.0 打开mongo shell -- 新建数据库test --( use test) 打开eclipse新建工程,把junit, ...