这是一道模板题。

读入一个长度为 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. 不用任何图片,只用简单的css写出唯美的钟表,就问你行吗?

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAGHCAIAAABJR31QAAAgAElEQVR4nOy9aXhc1ZUurPvcH7f73n ...

  2. How to: Set up Openswan L2TP VPN Server on CentOS 6

    Have you ever wanted to set up your own VPN server? By following the steps below, you can set up you ...

  3. 使用WebDriver遇到的那些坑(转)

    http://www.huangbowen.net/blog/2013/06/25/practice-of-webdriver/ 在做web项目的自动化端到端测试时主要使用的是Selenium Web ...

  4. js判空

    2014年9月3日 11:36:10 转载的: http://blog.sina.com.cn/s/blog_755168af0100vsik.html typeof用法 typeof的运算数未定义, ...

  5. Office 2010启动时出现无法验证此应用程序的许可证的解决

    Office 2010启动之后弹出一个窗口提示:Microsoft Office无法验证此应用程序的许可证.修复尝试失败或者已被用户取消.应用程序将立即关闭. 遇到这样的情况,原因是Office的系统 ...

  6. 8.python笔记之面向对象基础

    title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...

  7. cc.game

    概述 使用cc.game单例代替了原有的cc.Application以及cc.AppControl. cc.game是Cocos2d-JS的游戏对象,主要职责包括,配置的读取,引擎的加载,游戏脚本的加 ...

  8. Xshell 中文乱码

    Xshell对于嵌入式开发来说,是个非常不错的工具.但或许都有过被中文显示为乱码的问题感觉有点不爽.解决方法其实很简单的,即把xshell编码方式改成UTF-8即可. [文件]–>[打开]–&g ...

  9. Android之事件分发机制

    本文主要包括以下内容 view的事件分发 viewGroup的事件分发 首先来看两张图 在执行touch事件时 首先执行dispatchTouchEvent方法,执行事件分发. 再执行onInterc ...

  10. Android_adb shell am/pm使用

    转自:http://blog.sina.com.cn/s/blog_51335a0001017ux5.html   adb shell am instrument [options] <COMP ...