http://poj.org/problem?id=3581

这题说是给了N个数字组成的序列A1 A2 ..An 其中A1 大于其他的数字 , 现在要把序列分成三段并将每段分别反转求最小字典序

以后还是老老实实用基数排序 用sort wa 了 一晚

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const int maxn =;
struct SuffixArry
{
int c[maxn*],t[maxn*],t1[maxn*],sa[maxn*];
void build(int n,int m,int *S)
{
int *x=t,*y=t1,i;
for( i=; i<m; i++)c[i]=;
for( i=; i<n; i++)c[x[i]=S[i] ]++;
for( i=; i<m; i++)c[i]+=c[i-];
for( i=n-; i>=; i--)sa[ --c[ x[i] ] ]=i;
int p;
for(int k=; k<=n; k<<= )
{
p=;
for( i = n-k; i<n; i++)y[p++]=i;
for( i=; i<n; i++) if(sa[i]>=k)y[p++]=sa[i]-k;
for( i=; i<m; i++)c[ i ] = ;
for( i= ; i<n; i++ )c[ x[ y[i] ] ]++;
for( i=; i<m; i++) c[i]+=c[i-];
for( i=n-; i>=; i--) sa[--c[ x[ y[i] ] ] ]=y[i];
swap(x,y);
p=; x[ sa[ ] ] = ;
for(int i=; i<n; i++)
x[ sa[i] ] = y[ sa[i] ]== y[ sa[i-] ]&& y[ sa[i]+k ]==y[sa[i-]+k]?p-:p++;
if(p>=n)break;
m=p;
}
}
void clear(){
memset(t,-,sizeof(t));
memset(t1,-,sizeof(t1));
}
}T;
int A[maxn],B[maxn],C[maxn],rev[maxn*];
void revercopy(int *a, int *b, int len)
{
for(int i=; i<len; i++)
{
b[i]=a[len--i];
}
}
void rever(int *a, int len)
{
for(int i=; i<len/; i++ )
{
int t= a[i];
a[i]=a[len--i];
a[len--i]=t;
}
}
int main(int argc, char *argv[]) { int n;
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%d",&A[i]);
B[i]=A[i];
}
sort(B,B+n);
int L=unique(B,B+n)-B;
for(int i=; i<n; i++)
{
C[i]=lower_bound(B,B+L,A[i])-B;
}
int p1;
T.clear();
revercopy(C,rev,n);
T.build(n,L,rev); for(int i=; i<n; i++)
{
p1 = n-T.sa[i];
if(p1>&&n-p1>=)break;
} if(p1<||n-p1<)while(true){};
int m= n-p1;
revercopy(C+p1,rev,m); //for(int i=0; i<m; i++)printf("%d ",rev[i]);
revercopy(C+p1,rev+m,m);//for(int i=0; i<m*2; i++)printf("%d ",rev[i]);
T.build(m*,L,rev);
int p2;
for(int i=; i<m*; i++)
{
p2 = p1 + m - T.sa[ i ];
if(p2>p1&&n-p2>=)break;
}
if(p2<=p1||n-p2<)while(true){
};
rever(A,p1);
rever(A+p1,p2-p1);
rever(A+p2,n-p2);
for(int i=; i<n; i++) printf("%d\n",A[i]);
return ;
}

POJ3581 后缀数组的更多相关文章

  1. POJ3581 Sequence —— 后缀数组

    题目链接:https://vjudge.net/problem/POJ-3581 Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Su ...

  2. POJ3581 Sequence(后缀数组)

    题意:给一个串,串的第一个字符比后面的都大,要把它分成三段,然后反转每一段,求能得到的字典序最小的串是什么. 首先,第一段是可以确定的:把原串反转,因为第一个字符是最大的,它是唯一的,不存在反转串的后 ...

  3. 后缀数组的倍增算法(Prefix Doubling)

    后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...

  4. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  5. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  6. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  7. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  8. 后缀数组(suffix array)详解

    写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...

  9. 【UOJ #35】后缀排序 后缀数组模板

    http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...

随机推荐

  1. Python拷贝文件脚本

    author : headsen chen date : 2018-12-06  17:56:58 copy_file.py #!/usr/bin/env python from sys import ...

  2. 源码包安装Python3.6

    1,安装Python3.6的依赖包 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel r ...

  3. wireshark抓取OMCI报文

    1.安装文件: 1.1 BinDecHex.lua 1.2 omci.lua 2.如上两个文件copy至wireshark安装目录,如C:\Program Files (x86)\Wireshark ...

  4. 获取页面所有链接的JS

    写了一个实用的JS脚本,获取当前页面所有的JS: var str = " \n"; var list = document.getElementsByTagName("a ...

  5. HTTP与HTTPS对访问速度(性能)的影响【转】

    1 前言 HTTPS 在保护用户隐私,防止流量劫持方面发挥着非常关键的作用,但与此同时,HTTPS 也会降低用户访问速度,增加网站服务器的计算资源消耗. 本文主要介绍 https 对用户体验的影响. ...

  6. HDU 1281 - 棋盘游戏 - [二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1281 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  7. git 出现 refusing to merge unrelated histories 解决

    如果出现refusing to merge unrelated histories,使用以下方法即可 git pull origin master --allow-unrelated-historie ...

  8. android 控制POS机图文打印(二)

    上一篇文章结束了ESC/POS的指令集,没看过的可以去看一下,可以当作工具文档来使用的 android 控制POS机图文打印(一) 这一篇正式介绍如何使用POS机来打印图文信息. 首先介绍一下,ESC ...

  9. Rikka with Parenthesis II---hdu5831(括号匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5831 给你一个只包含‘(’‘)’的字符串,然后让我们交换两个字符一次,问是否能得到一个合法的匹配:必须 ...

  10. 统计难题---hdu1251字典树模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 node *head=(node*)malloc(sizeof(node)); for(int ...