UOJ#35 后缀排序
这是一道模板题。
读入一个长度为 n 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置。位置编号为 1 到 n。
除此之外为了进一步证明你确实有给后缀排序的超能力,请另外输出 n−1 个整数分别表示排序后相邻后缀的最长公共前缀的长度。
输入格式
一行一个长度为 n 的仅包含小写英文字母的字符串。
输出格式
第一行 n 个整数,第 i 个整数表示排名为 i 的后缀的第一个字符在原串中的位置。
第二行 n−1 个整数,第 i 个整数表示排名为 i 和排名为 i+1 的后缀的最长公共前缀的长度。
样例一
input
ababa
output
5 3 1 4 2
1 3 0 2
explanation
排序后结果为:
- a
- aba
- ababa
- ba
- baba
限制与约定
1≤n≤105
时间限制:1s
空间限制:256MB
我的后缀平衡树终于过了
#include<cstdio>
#include<cctype>
#include<queue>
#include<ctime>
#include<cstring>
#include<algorithm>
#define mid l+r>>1
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
typedef long long ll;
int h[maxn],ch[maxn][],c[maxn],tot,rt;
ll rk[maxn];
void rebuild(int o,ll l,ll r) {
if(!o) return;rk[o]=l+r;
rebuild(ch[o][],l,mid);rebuild(ch[o][],mid,r);
}
void rotate(int& o,int d,ll l,ll r) {
int k=ch[o][d^];ch[o][d^]=ch[k][d];ch[k][d]=o;o=k;
rebuild(o,l,r);
}
int cmp(int x,int y) {return c[x]<c[y]||(c[x]==c[y]&&rk[x-]<rk[y-]);}
void insert(int& o,ll l,ll r) {
if(!o) {o=tot;rk[o]=l+r;return;}
if(cmp(tot,o)) {insert(ch[o][],l,mid);if(h[ch[o][]]>h[o]) rotate(o,,l,r);}
else {insert(ch[o][],mid,r);if(h[ch[o][]]>h[o]) rotate(o,,l,r);}
}
void insert(int x) {
c[++tot]=x;h[tot]=rand()*rand();
insert(rt,,1ll<<);
}
void print(int o) {
if(!o) return;
print(ch[o][]);
printf("%lld ",rk[o]);
print(ch[o][]);
}
int n,sa[maxn],rank[maxn],height[maxn];
int cmp2(int a,int b) {return rk[n-a+]<rk[n-b+];}
char s[maxn];
int main() {
srand(time());
scanf("%s",s);n=strlen(s);
rep(,n) insert(s[n-i]-'a'+),sa[i]=i;
sort(sa+,sa+n+,cmp2);
rep(,n) rank[sa[i]]=i,printf("%d ",sa[i]);puts("");
for(int i=,j,k=;i<=n;i++) {
j=sa[rank[i]-];
while(s[i+k-]==s[j+k-]) k++;
height[rank[i]]=k?k--:k;
}
rep(,n) printf("%d ",height[i]);
return ;
}
UOJ#35 后缀排序的更多相关文章
- UOJ #35. 后缀排序[后缀数组详细整理]
#35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符 ...
- Uoj #35. 后缀排序(后缀数组)
35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在 ...
- UOJ #35. 后缀排序 后缀数组 模板
http://uoj.ac/problem/35 模板题,重新理了一遍关系.看注释吧.充分理解了倍增的意义,翻倍之后对上一次排序的利用是通过一种类似于队列的方式完成的. #include<ios ...
- UOJ #35 后缀排序 哈希做法
题面 http://uoj.ac/problem/35 题解 后缀数组当然可以 这里用哈希做 首先排序的问题在哪里 在于比较两个后缀的复杂度是O(length)的 但是我们可以通过找LCP来优化比较 ...
- UOJ#35 —— 后缀排序
1.题目大意:后缀数组模板题 2.分析:汝佳的书上的代码的有bug,还有那个n是字符串长度+1,''也要加入排序的 存个模板QAQ #include <cstdio> #include & ...
- 【后缀数组】uoj#35. 后缀排序
模板 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #de ...
- uoj#35 后缀排序(后缀数组模版)
#include<bits/stdc++.h> #define N 100005 using namespace std; char s[N]; int a[N],c[N],t1[N],t ...
- UOJ 35 后缀数组
后缀数组裸题,求排名第1~n的后缀,想相邻后缀的最长公共前缀. 集训队模板就是硬lO(∩_∩)O哈哈~ #include <cstdio> #include <cmath> # ...
- 洛谷.3809.[模板]后缀排序(后缀数组 倍增) & 学习笔记
题目链接 //输出ht见UOJ.35 #include<cstdio> #include<cstring> #include<algorithm> const in ...
随机推荐
- HM必修1
高中数学必修一 笔记与拓展 1. 集合与函数概念 集合概念 集合是一个基本的数学概念. 集合是由集合的元素构成的. 当且仅当两个集合中包含着完全相同的元素且都不包含其它元素时两个集合相等. 集合是确定 ...
- 【系统】CentOS、Ubuntu、Debian三个linux比较异同
CentOS.Ubuntu.Debian三个linux比较异同 2014-07-31 12:58 53428人阅读 评论(6) ...
- 【OpenStack】OpenStack系列8之Nova详解 Neutron详解
Neutron下载安装 下载:git clone -b stable/icehouse https://github.com/openstack/neutron.git pip install -r ...
- BASH相关
颜色 http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html http://segmentfault.com/q/10100000 ...
- JavaScript toFixed()使用的注意事项
以下是w3school的定义: 定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法 NumberObject.toFixed(num) 参数 描述 num 必 ...
- 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- Ubuntu 12.10 安装 jdk-7u10-linux-x64.tar.gz(转载)
在Ubuntu 12.10下安装 jdk-7u10-linux-x64.tar.gz 总的原则:将jdk-7u10-linux-x64.tar.gz压缩包解压至/usr/lib/jdk,设置jdk环境 ...
- python基础——使用dict和set
python基础——使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其它语言中也称为map(映射),使用键-值(key-value)存储,具 ...
- useradd mfs -s /sbin/nologin -M
创建用户但不建家目录
- Redis笔记(二)Redis的部署和启动
Linux下Redis的部署和启动 下载安装介质 Redis官网地址:http://www.redis.io/目前最新版本是redis-3.0.3. 可以访问 http://download.redi ...