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. 使用HtmlAgilityPack解析html

    HtmlAgilityPack是.net下使用xPath来解析html的类库,可以方便的做html的页面分析处理 项目地址: http://htmlagilitypack.codeplex.com/ ...

  2. 从Java代码到字节码(1)

    理解Java代码是如何被编译为字节码并在Java虚拟机(JVM)上执行是非常重要的,这将帮助理解你的程序是如何执行的.这样的理解不仅仅能够让你在逻辑上更好的掌握语言特性,而且能够有机会理解在做出重要决 ...

  3. mysql补充(3)优化sql语句查询常用的30种方法

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...

  4. vue--自定义验证指令

    参考文档: https://cn.vuejs.org/v2/guide/custom-directive.html https://www.cnblogs.com/ilovexiaoming/p/68 ...

  5. 纯CSS绘制的三角形箭头图案【原创】

    参考:http://www.webhek.com/css-triangles/ 使用上下左右的三角形箭头标志,直接用css即可完成,直接附上代码. css: div#up { width: 0px; ...

  6. 9.5Django

    2018-9-5 15:23:00 配置数据库信息  setting MySQLdb 不支持python3 创建表 pycharm 连接数据库 好强大的赶脚

  7. CSU 1808 - 地铁 - [最短路变形]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 Time limit: 5000 ms Memory limit: 13107 ...

  8. 关于graham扫描法求凸包的小记

    1.首先,凸包是啥: 若是在二维平面上,则一般的,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点. ───────────────────────────── ...

  9. compile time - run-time

    php.net Class member variables are called "properties". You may also see them referred to ...

  10. 【Git 使用笔记】第三部分:多分支开发

    ###举例仓库 仓库地址A:git@gitlab.54php.cn:guowei/demos.git 仓库地址B:git@gitlab.54php.cn:infra/demos.git 开发人员仓库C ...