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\)个非空段,并将每一段分别反转,求能得到的字典序最小的序列 ...
随机推荐
- Android SimpleAdapter ListView (锁定手机,解锁手机的列表)
SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局. 构造方法: SimpleAdapter(Context context, List<? extends Map< ...
- oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
LAG()和LEAD()统计函数能够在一次查询中取出同一字段的前N行的数据和后N行的值.这样的操作能够使用对同样表的表连接来实现,只是使用LAG和 LEAD有更高的效率.下面整理的LAG()和LEAD ...
- C# 将MSMQ消息转换成Json格式 【优化】
C# 将MSMQ消息转换成Json格式 [优化] 转换函数: private string ConvertToJSON(string label, string body) { //TODO: co ...
- OleDb 读取 excel
//定义变量 _Application ExcelApp; Workbooks wbsMyBooks; _Workbook wbMyBook; Worksheets wssMysheets; _Wor ...
- html 第一阶段 学习使用总结
基本使用内容: <html> <head> <title>Title of the document</title> <link rel=&quo ...
- MediaPlayer 音频播放 示例
状态机.流程图.生命周期 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作驱动的生命周期和状态. 椭圆代表MediaPlayer对象可 ...
- adb服务无法启动
今天学习android编程发现调试出错 The connection to adb is down, and a severe error has occured. You must restart ...
- JQuery图片轮换 nivoSlider图片轮换
效果图: 第一步:添加引用 <script src="jquery-ui-1.9.2.custom.min.js" type="text/javascript&qu ...
- pointer-events属性
pointer-events的风格更像JavaScript,它能够: 1.阻止用户的点击动作产生任何效果.阻止缺省鼠标指针的显示3.阻止CSS里的hover和active状态的变化触发事件4.阻止Ja ...
- apache 2.4 You don't have permission to access / on this server
用的2.4版本,以前版本解决: 马上打开apache的配置文件httpd.conf,逐行检查.在大约快一半的地方有以下这段代码: <Directory /> Options Foll ...