UOJ#35 —— 后缀排序
1、题目大意:后缀数组模板题
2、分析:汝佳的书上的代码的有bug,还有那个n是字符串长度+1,‘’也要加入排序的
存个模板QAQ
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char s[101000];
int sa[101000], t[101000], t2[101000], c[101000];
int Rank[101000], height[101000];
inline void build_sa(int m){
int *x = t, *y = t2;
int n = strlen(s) + 1;
for(int i = 0; i < m; i ++) c[i] = 0;
for(int i = 0; i < n; i ++) c[x[i] = s[i]] ++;
for(int i = 1; i < m; i ++) c[i] += c[i - 1];
for(int i = n - 1; i >= 0; i --) sa[-- c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1){
int p = 0;
for(int i = n - k; i < n; i ++) y[p ++] = i;
for(int i = 0; i < n; i ++) if(sa[i] >= k) y[p ++] = sa[i] - k;
for(int i = 0; i < m; i ++) c[i] = 0;
for(int i = 0; i < n; i ++) c[x[y[i]]] ++;
for(int i = 0; i < m; i ++) c[i] += c[i - 1];
for(int i = n - 1; i >= 0; i --) sa[-- c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(int i = 1; i < n; i ++){
if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]) x[sa[i]] = p - 1;
else x[sa[i]] = p ++;
}
if(p >= n) break;
m = p;
}
for(int i = 1; i < n; i ++) printf("%d ", sa[i] + 1);
printf("\n");
return;
}
inline void getHeight(){
int k = 0;
int n = strlen(s) + 1;
for(int i = 0; i < n; i ++) Rank[sa[i]] = i;
for(int i = 0; i < n; i ++){
if(k) k --;
if(!Rank[i]) continue;
int j = sa[Rank[i] - 1];
while(s[i + k] == s[j + k] && i + k < n && j + k < n) k ++;
height[Rank[i]] = k;
}
for(int i = 2; i < n; i ++) printf("%d ", height[i]);
printf("\n");
}
int main(){
scanf("%s", s);
build_sa(300);
getHeight();
return 0;
}
UOJ#35 —— 后缀排序的更多相关文章
- UOJ #35. 后缀排序[后缀数组详细整理]
#35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符 ...
- Uoj #35. 后缀排序(后缀数组)
35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在 ...
- UOJ#35 后缀排序
这是一道模板题. 读入一个长度为 n 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编号为 1 到 n. 除此之外为 ...
- UOJ #35. 后缀排序 后缀数组 模板
http://uoj.ac/problem/35 模板题,重新理了一遍关系.看注释吧.充分理解了倍增的意义,翻倍之后对上一次排序的利用是通过一种类似于队列的方式完成的. #include<ios ...
- UOJ #35 后缀排序 哈希做法
题面 http://uoj.ac/problem/35 题解 后缀数组当然可以 这里用哈希做 首先排序的问题在哪里 在于比较两个后缀的复杂度是O(length)的 但是我们可以通过找LCP来优化比较 ...
- 【后缀数组】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 ...
随机推荐
- Linux 常用命令笔记 (持续更新)
声明:本文是转载前辈的,地址:http://www.cnblogs.com/tovep/articles/2473147.html 在tomcat的bin目录下执行 ./shutdown.sh 为了查 ...
- uC/OS-II任务(OS_task)块
/*************************************************************************************************** ...
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)
一.软件介绍(apache lighttpd nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...
- 第二章 存储,2.2 AliCloudDB--双11商家后台数据库的基石(作者:玄惭)
2.2 AliCloudDB--双11商家后台数据库的基石 前言 2016年天猫双11购物狂欢节已经完美落下帷幕,千亿成交的背后,作为整个天猫商家后台数据库的基石,AliCloudDB是如何保障在零点 ...
- webkit的一些不为人知的高级属性
1.-webkit-tap-highlight-color tap按钮或者链接时,就会出现一个半透明的灰色背景,设置属性: -webkit-tap-highlight-color:transpar ...
- Http常用状态码
HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释:点击查看 1xx(临时响应 ...
- AutoMapper.EF6
https://github.com/AutoMapper/AutoMapper.EF6 Extensions for AutoMapper and EF6 This contains some us ...
- 从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构
Microsoft.AspNet.Identity简介 Microsoft.AspNet.Identity是微软在MVC 5.0中新引入的一种membership框架,和之前ASP.NET传统的mem ...
- mysql数据表分表策略2(转)
mysql分表方法: 方法一. 做数据库集群! 主从数据库 双向热备份(或一对多的数据库实时备份策略),这样可将数据库查询分摊到几个服务器去(可跟服务器负载均衡结合起来架构) 优点:扩展性好,没有多个 ...
- 常用的MIME类型
.doc application/msword .docx application/vnd.openxmlformats-officedocument.wordprocessingml.d ...