这是一道模板题。

读入一个长度为 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

排序后结果为:

  1. a
  2. aba
  3. ababa
  4. ba
  5. 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 后缀排序的更多相关文章

  1. UOJ #35. 后缀排序[后缀数组详细整理]

    #35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符 ...

  2. Uoj #35. 后缀排序(后缀数组)

    35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在 ...

  3. UOJ #35. 后缀排序 后缀数组 模板

    http://uoj.ac/problem/35 模板题,重新理了一遍关系.看注释吧.充分理解了倍增的意义,翻倍之后对上一次排序的利用是通过一种类似于队列的方式完成的. #include<ios ...

  4. UOJ #35 后缀排序 哈希做法

    题面 http://uoj.ac/problem/35 题解 后缀数组当然可以 这里用哈希做 首先排序的问题在哪里 在于比较两个后缀的复杂度是O(length)的 但是我们可以通过找LCP来优化比较 ...

  5. UOJ#35 —— 后缀排序

    1.题目大意:后缀数组模板题 2.分析:汝佳的书上的代码的有bug,还有那个n是字符串长度+1,''也要加入排序的 存个模板QAQ #include <cstdio> #include & ...

  6. 【后缀数组】uoj#35. 后缀排序

    模板 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #de ...

  7. uoj#35 后缀排序(后缀数组模版)

    #include<bits/stdc++.h> #define N 100005 using namespace std; char s[N]; int a[N],c[N],t1[N],t ...

  8. UOJ 35 后缀数组

    后缀数组裸题,求排名第1~n的后缀,想相邻后缀的最长公共前缀. 集训队模板就是硬lO(∩_∩)O哈哈~ #include <cstdio> #include <cmath> # ...

  9. 洛谷.3809.[模板]后缀排序(后缀数组 倍增) & 学习笔记

    题目链接 //输出ht见UOJ.35 #include<cstdio> #include<cstring> #include<algorithm> const in ...

随机推荐

  1. [ruby on rails] 跟我学之(8)修改数据

    修改views 修改index视图(app/views/posts/index.html.erb),添加编辑链接,如下: <h1>Our blogs</h1> <% @p ...

  2. Linux&shell之高级Shell脚本编程-创建函数

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用函数 #!/bin/bash # testing the script function myfun { echo "This i ...

  3. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. Notice The minimu ...

  4. SharePoint 2010 隐藏快速启动栏之使用内容编辑器webpart

    SharePoint 2010 自带的webpart里有一个叫内容编辑,在媒体和内容分类里面: 将其添加到页面后效果: 点击用于添加新内容,此时注意Ribbon菜单中的变化: 这里可以看到,你可以插入 ...

  5. 无论IT代码系统还是人生都是有惯性的

    是的,这和IT系统和代码没有什么关系:鸡汤式的文章,看烦了的就关掉吧,想看的请听我碎碎念.惯性本是物理学研究的问题,这里没有要研究物理学里的惯性.惯性无时无刻地发生在我们的日常生活中,只是你我都没有察 ...

  6. python为什么有私有方法和变量

    1. 访问安全,其实也没有决定的安全 >>> class humer(object): ... def __init__(self, name): ... self.name = n ...

  7. Android学习笔记——文件路径(/mnt/sdcard/...)、Uri(content://media/external/...)学习

    一.URI 通用资源标志符(Universal Resource Identifier, 简称"URI"). Uri代表要操作的数据,Android上可用的每种资源 - 图像.视频 ...

  8. rsync实时同步文件

    http://rsync.samba.org/download.html [root@v01 src]# yum install git [root@v01 src]# git clone git:/ ...

  9. 搭建邮局(邮件服务器) - hmailserver

    1.查看服务器mx是否解析成功 nslookup  set type=mx   2.hmailserver服务器 smtp设置     3.foxmail 设置   4.使用webmail(after ...

  10. JavaScript开发中的一些问题

    1.求y和z的值是多少? <script type=”text/javascript”> var x = 1; var y = 0; var z = 0; function add(n){ ...