POJ3581---Sequence 后缀树组
题意:n个数字组成的序列,第一个数字最大,,把序列分成3部分,每个部分分别翻转,输出翻转后字典序最小的序列。。
后缀数组变一下,,先求出 第一个分割的位置,,然后再求一次后缀数组,,求出第二个位置。。输出就好了。
此题要采用单组输入。。。
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 2e5+;
int s[maxn], rev_s[maxn << ];
int sa[maxn], rank[maxn], tmp[maxn];
int k, sort_len;
bool cmp(int i, int j)
{
if (rank[i] != rank[j])
return rank[i] < rank[j];
else
{
int x = i + k <= sort_len ? rank[i+k] : -;
int y = j + k <= sort_len ? rank[j+k] : -;
return x < y;
}
}
void build_sa(int str[], int len)
{
sort_len = len;
for (int i = ; i <= len; i++)
{
sa[i] = i;
rank[i] = i < len ? str[i] : -;
}
for (k = ; k <= len; k *= )
{
sort(sa, sa + len + , cmp);
tmp[sa[]] = ;
for (int i = ; i <= len; i++)
{
tmp[sa[i]] = tmp[sa[i-]] + (cmp(sa[i-],sa[i]) ? : );
}
for (int i = ; i <= len; i++)
rank[i] = tmp[i];
}
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
//while (~scanf ("%d", &n))
scanf ("%d", &n);
{
for (int i = ; i < n; i++)
scanf ("%d", s + i);
reverse_copy(s, s + n, rev_s);
build_sa(rev_s, n);
int pos1;
for (int i = ; i <= n; i++)
{
pos1 = n - sa[i] - ;
if (pos1 >= && pos1 <= n - )
break;
}
int len = n - pos1 - ;
reverse_copy(s+pos1+, s+n, rev_s);
reverse_copy(s+pos1+, s+n, rev_s+len);
build_sa(rev_s, len << );
int pos2;
for (int i = ; i <= * len; i++)
{
pos2 = len - sa[i] - ;
if (sa[i] < len && pos1++pos2 < n-)
break;
}
for (int i = pos1; i >= ; i--)
printf("%d\n",s[i]);
for (int i = pos2+pos1+; i > pos1; i--)
printf("%d\n",s[i]);
for (int i = n-; i > pos2+pos1+; i--)
printf("%d\n", s[i]);
}
return ;
}
POJ3581---Sequence 后缀树组的更多相关文章
- 【BZOJ-1396&2865】识别子串&字符串识别 后缀自动机/后缀树组 + 线段树
1396: 识别子串 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 312 Solved: 193[Submit][Status][Discuss] ...
- POJ3581 Sequence —— 后缀数组
题目链接:https://vjudge.net/problem/POJ-3581 Sequence Time Limit: 5000MS Memory Limit: 65536K Total Su ...
- POJ3581:Sequence(后缀数组)
Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An, you are to ...
- FZU 2137 奇异字符串 后缀树组+RMQ
题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...
- SPOJ694 -- DISUBSTR 后缀树组求不相同的子串的个数
DISUBSTR - Distinct Substrings Given a string, we need to find the total number of its distinct su ...
- CF504E Misha and LCP on Tree(树链剖分+后缀树组)
1A真舒服. 喜闻乐见的树链剖分+SA. 一个初步的想法就是用树链剖分,把两个字符串求出然后hash+二分求lcp...不存在的. 因为考虑到这个字符串是有序的,我们需要把每一条重链对应的字符串和这个 ...
- HDU4436---str2int 后缀树组(12年天津区域赛)
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- [转载]字典树(trie树)、后缀树
(1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...
- [POJ3581]Sequence
[POJ3581]Sequence 题目大意: 给定序列\(A_{1\sim n}\),其中\(A_1\)为最大的数.要把这个序列分成\(3\)个非空段,并将每一段分别反转,求能得到的字典序最小的序列 ...
随机推荐
- 【java】Windows7 下设置环境变量
Windows 7下配置JDK环境变量參数设置: 1. 安装JDK,安装过程中能够自己定义安装文件夹等信息,比如我们选择安装文件夹为:D:\Program Files (x86)\Java\jd ...
- [Angular 2] Mapping Streams to Values to Affect State
While you have multiple streams flowing into your scan operator, you'll need to map each stream to t ...
- Android 自定义View (一)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...
- Mybatis插入语句useGeneratedKeys="true"的用法
<!-- 插入新的问题件 --> <!-- useGeneratedKeys="true"把新增加的主键赋值到自己定义的keyProperty(id)中 --&g ...
- PV和并发
几个概念 网站流量是指网站的访问量,用来描述访问网站的用户数量以及用户所浏览的网页数量等指标,常用的统计指标包括网站的独立用户数量.总用户数量(含重复访问者).网页浏览数量.每个用户的页面浏览数量.用 ...
- 【转】iOS实时卡顿监控
转自http://www.tanhao.me/code/151113.html/ 在移动设备上开发软件,性能一直是我们最为关心的话题之一,我们作为程序员除了需要努力提高代码质量之外,及时发现和监控软件 ...
- nginx配置学习文章
partOne 自我释义部分 我的是阿里云的ubuntu *******实际上感觉这里是基本配置,很用不到*********#定义其用户或用户组user www-data;#nginx的进程数,应当为 ...
- HTML5触摸屏touch事件使用实例1
1.源码: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...
- ASP.NET获取根目录的方法集合
编写程序的时候,经常需要用的项目根目录,自己总结如下: 1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 方法 ...
- linux下查看已经安装的jdk 并卸载jdk
一.查看Jdk的安装路径: whereis javawhich java (java执行路径)echo $JAVA_HOME echo $PATH 备注:如果是windows中,可以使用: set j ...