后缀数组(板子)
Posted maomao9173
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后缀数组(板子)相关的知识,希望对你有一定的参考价值。
贴一下防止忘掉。
#include <bits/stdc++.h>
#define N 1000010
using namespace std;
char s[N];
int n, m, sa[N], tp[N], rk[N], _rk[N], bin[N];
void base_sort () {
for (int i = 1; i <= m; ++i) bin[i] = 0;
for (int i = 1; i <= n; ++i) bin[rk[tp[i]]]++; //每个出现的第一关键字++
for (int i = 1; i <= m; ++i) bin[i] += bin[i - 1]; //前缀和 -> rk为i的后缀排名是多少
for (int i = n; i >= 1; --i) sa[bin[rk[tp[i]]]--] = tp[i]; // 先记大的,然后名次顺位递减
}
void suffix_sort () {
m = 255;
for (int i = 1; i <= n; ++i) {
rk[i] = s[i - 1], tp[i] = i;
}
base_sort ();
for (int w = 1; w <= n; w <<= 1) {
int cnt = 0;
for (int i = n - w + 1; i <= n; ++i) {
tp[++cnt] = i; //莫得第二关键字
}
for (int i = 1; i <= n; ++i) {
if (sa[i] > w) { //如果有第二关键字
tp[++cnt] = sa[i] - w; //把i当做第二关键字の后缀
}
}
//printf ("sa : "); for (int i = 1; i <= n; ++i) printf ("%d ", sa[i]); printf ("
");
//printf ("rk : "); for (int i = 1; i <= n; ++i) printf ("%d ", rk[i]); printf ("
");
base_sort (); cnt = 1;
//printf ("sa : "); for (int i = 1; i <= n; ++i) printf ("%d ", sa[i]); printf ("
");
//printf ("rk : "); for (int i = 1; i <= n; ++i) printf ("%d ", rk[i]); printf ("
");
memcpy (_rk, rk, sizeof (rk)); //把rk临时放一边
rk [sa[1]] = 1;
for (int i = 2; i <= n; ++i) {
rk[sa[i]] = _rk[sa[i]] == _rk[sa[i - 1]] && _rk[sa[i] + w] == _rk[sa[i - 1] + w] ? cnt : ++cnt;
//验证后缀sa[i]是不是和sa[i - 1]相等
}
if (cnt == n) break; //finished
m = cnt; //m的可用范围
}
}
int main () {
scanf ("%s", s);
n = strlen (s);
printf ("n = %d
", n);
suffix_sort ();
for (int i = 1; i <= n; ++i) {
printf ("%d ", sa[i]);
}
return 0;
}
总结流程:
最初 : 推出(1-sa)和(1-rk)
过程 :
根据(w-sa)推(w-tp)
根据(w-tp)和(w-rk)推(2w-sa)
根据(2w-sa)和(w-rk)推(2w-rk)
以上是关于后缀数组(板子)的主要内容,如果未能解决你的问题,请参考以下文章