标签:name 后缀 scanf turn 过程 临时 scan eof space
#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 ("\n");
//printf ("rk : "); for (int i = 1; i <= n; ++i) printf ("%d ", rk[i]); printf ("\n");
base_sort (); cnt = 1;
//printf ("sa : "); for (int i = 1; i <= n; ++i) printf ("%d ", sa[i]); printf ("\n");
//printf ("rk : "); for (int i = 1; i <= n; ++i) printf ("%d ", rk[i]); printf ("\n");
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", n);
suffix_sort ();
for (int i = 1; i <= n; ++i) {
printf ("%d ", sa[i]);
}
return 0;
}
标签:name 后缀 scanf turn 过程 临时 scan eof space
原文地址:https://www.cnblogs.com/maomao9173/p/10362172.html