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. CodeFirst Update-Database 出现对象'DF__**__**__**' 依赖于 列'**'。

    今天在使用Mirgration更新数据表时,出现这样一个错误 经排查,是由于CodeFirst在创建数据库时会为不可为null的字段创建默认值约束 只要在数据库中删除这个约束就可以解决

  2. HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)

    Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...

  3. poj2492 A Bug's Life【并查集】

    Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...

  4. 《机器学习实战》中的程序清单2-1 k近邻算法(kNN)classify0都做了什么

    from numpy import * import operator import matplotlib import matplotlib.pyplot as plt from imp impor ...

  5. 卿学姐与公主 UESTC - 1324 分块模板题

    题意:http://acm.uestc.edu.cn/#/problem/show/1324 中文题,自己看喽. 题解:分块模板,update时顺便更新块属性.ask时先判掉belong[l]==be ...

  6. 疯狂java讲义 第三版 笔记

      java7新加特性: 0B010101  二进制数 int c=0B0111_1111;   数值中使用下划线分隔 switch 支持String类型   字符串常量放在常量池 String s0 ...

  7. numpy、pandas、scipy介绍

    https://blog.csdn.net/LOLITA0164/article/details/80195124 numpy简介NumPy(Numeric Python)是一个Python包.它是一 ...

  8. PL/SQL常用语法及举例

    PLSQL语句 DECLARE 声明部分 BEGIN 程序编写,SQL语句 EXECPTION 处理异常 END; / 声明部分(DECLARE) SQL> set serveroutput o ...

  9. LoadRunner-关联(自动关联和手动关联)

    使用LR录制脚本,新建课程:每次新建课程都会产生一个新的courseid和partid,直接使用录制的脚本执行是不行的.所以用到了关联. 注:关联分手动和自动关联,自动关联搜索出一些不必要关联的数据, ...

  10. Rotate Image(二位数组顺时针旋转)

    问题描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...