后缀数组

代码

void rsort() {
for (int i = 1; i <= m; ++i) tax[i] = 0;
for (int i = 1; i <= n; ++i) ++tax[rnk[i]];
for (int i = 1; i <= m; ++i) tax[i] += tax[i-1];
for (int i = n; i >= 1; --i) sa[tax[rnk[tmp[i]]]--] = tmp[i];
} void ssort() {
for (int i = 1; i <= n; ++i) rnk[i] = a[i], tmp[i] = i;
m = 127;
rsort();
for (int w = 1, p = 0; p < n; w <<= 1) {
p = 0;
for (int i = 1; i <= w; ++i) tmp[++p] = n - w + i;
for (int i = 1; i <= n; ++i) if (sa[i] > w) tmp[++p] = sa[i] - w;
rsort();
std::swap(rnk, tmp);
rnk[sa[1]] = p = 1;
for (int i = 2; i <= n; ++i) {
rnk[sa[i]] = (tmp[sa[i]] == tmp[sa[i-1]]
&& tmp[sa[i]+w] == tmp[sa[i-1]+w]) ? p : ++p;
}
m = p;
}
for (int i = 1, k = 0; i <= n; ++i) {
while (a[i+k] == a[sa[rnk[i]-1]+k]) ++k;
h[rnk[i]] = k;
if (k) --k;
}
}

应用

关于后缀数组和后缀自动机,在hihocoder上有一套很好的题(重复旋律)。

最长可重叠重复K次子串问题

(hiho1403)

h数组中长度为k的子串的最小值的最大值。

最长不可重叠重复子串问题

(hiho1407)

二分答案为k,若h数组中有连续的一段大于k的值(即有一个子串重复了),且这一段中最靠前的位置和最靠后的位置之间的差大于k(即这个子串可以不重叠),那么该答案合法。

bool check(int x) {
int mn = N + 10, mx = 0;
for (int i = 1, flag = 0; i <= n; ++i) {
if (h[i] >= x) {
if (!flag) { // mark
mx = std::max(mx, sa[i-1]);
mn = std::min(mn, sa[i-1]);
}
mx = std::max(mx, sa[i]);
mn = std::min(mn, sa[i]);
flag = 1;
} else if (flag) {
flag = 0;
if (mx - mn >= x) {
return true;
}
mn = N + 10;
mx = 0;
}
}
return false;
}

注意由于h数组的定义,我们需要标记为mark的部分。

最长公共子串问题

(hiho1415)

将两个子串拼接起来,用'#'分隔,那么两个串的最长公共子串就是保证sa[i]和sa[i-1]不在同一个串内的最大的h[i]。

连续重复次数最多的子串

(hiho1419)

枚举子串长度l和重复起点p,计算重复次数lcp(p, p+l)/l + 1,复杂度\(O(n^2)\)。

考虑优化,我们可以以l的间隔枚举p,考虑某个位置p,记lcp(p, p+l)为R,那么,被我们忽略掉的位置p-1,p-2,p-3...的答案值不会超过R+1。

对于\(p-R\bmod l < x < p\) 的\(x\),以x为起点的答案值不可能超过R(由公式易得),而对于\(p-l<x<p-R\bmod l\)的\(x\),以x为起点的答案值也不可能超过以p-R%l的答案值,所以只需计算成倍的p和p-R%l的答案值即可。

for (int l = 1; l <= n; ++l) {
for (int i = 1; i+l <= n; i += l) {
int R = lcp(i, i + l);
ans = std::max(ans, R / l + 1);
if (i >= l - R%l) {
ans = std::max(ans,
lcp(i - l + R%l, i + R%l) / l + 1);
}
}
}

不同子串的数目问题

\(\frac{1}{2}n(n+1)-\sum_{i=1}^n h[i]\)

[Note]后缀数组的更多相关文章

  1. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  2. HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...

  3. 后缀数组---Musical Theme

    POJ   1743 Description A musical melody is represented as a sequence of N (1<=N<=20000)notes t ...

  4. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)

    永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...

  5. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  6. hdu 5442 Favorite Donut 后缀数组

    Favorite Donut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  7. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

  8. POJ1743---Musical Theme(+后缀数组二分法)

    Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are int ...

  9. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

随机推荐

  1. PAT (Basic Level) Practice (中文)1087 有多少不同的值 (20 分) (set)

    当自然数 n 依次取 1.2.3.…….N 时,算式 ⌊ 有多少个不同的值?(注:⌊ 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分.) 输入格式: 输入给出一个正整数 N(2). 输出 ...

  2. 问题 E: Problem B

    #include <cstdio> #include <cstring> #include <algorithm> #include <vector> ...

  3. Spring Batch 批处理原则与建议

    Spring Batch 批处理原则与建议 当我们构建一个批处理的过程时,必须注意以下原则: 通常情况下,批处理的过程对系统和架构的设计要够要求比较高,因此尽可能的使用通用架构来处理批量数据处理,降低 ...

  4. H5_0012:js事件冒泡和捕获

    捕获(capture)和冒泡(bubble)是事件传播过程中的两个概念, 比如用户单击某个元素, 但由于元素处于父元素内, 该父元素又处于document对象中, document对象又处于windo ...

  5. Ubuntu系统测评

    首次使用ubuntun系统 华为云可以免费试用30天,嘻嘻,正好熟悉一下linux命令 1.登录 login: 先输入用户名:root 在输入密码:******** 这个是在配置云服务器的时候自己设置 ...

  6. sqlserver删除所有表、视图、存储过程

    declare proccur cursor     for         select [name] from sysobjects where type='P' declare @procnam ...

  7. start.sh在linux下启动报错 Can't connect to any repository: ,cannot open git-receive-pack

    个人博客 地址:http://www.wenhaofan.com/article/20181223135418 报错信息 Can't connect to any repository: ,canno ...

  8. IntelliJ IDEA 2019.1.3 最新破解教程【最强 可用至2099年】

    本文包括最新[2019.1.2]安装 和[2018.3.2](推荐)安装 ①IntelliJ IDEA 2018.3.2安装永久安装[最强]  一. 在官网下载IDEA安装包  链接:https:// ...

  9. 你所不知道的locust

    from locust import HttpLocust, TaskSet, task import uuid, time import logging,json # https://docs.lo ...

  10. Ubuntu下vsc+python3配置

    我发现直接搜这个很容易得到过时的文章py2请滚蛋好么 1.首先下去VSC官网下载.deb的安装包,安装好vsc,打开后安装PYTHON,reload下.别啊忘记修改路径,setting下搜python ...