后缀数组(SA)备忘

2009集训队论文 网上的经典应用都是从里面抄的，还把解释给去掉了。。。真事屑

，这里面要减去k的原因是 sa[i] 作为 sa[i]-k 的第二关键字

"y[i]表示第二关键字排名为i的数，第一关键字的位置"

trick：

get_SA里的Y和rnk每次用完要清零 否则多组数据会出锅，字符串n+1位置记得弄成0

struct SA {
int k, sa[N], rnk[N], H[N], st[N][17];
char s[N];
bool cmp(int *y, int a, int b, int m) {return y[a] == y[b] && y[a + m] == y[b + m];}
void Sort(int *x, int *y, int *rk) {
static int C[N];
for (int i = 0; i <= k; ++i) C[i] = 0;
for (int i = 1; i <= n; ++i) ++C[rk[i]];
for (int i = 1; i <= k; ++i) C[i] += C[i - 1];
for (int i = n; i; --i) y[C[rk[x[i]]]--] = x[i];
}
void get_SA() {
static int Y[N];
int *y = Y, *rk = rnk;
k = 128;
for (int i = 1; i <= n; ++i) rk[i] = s[y[i] = i];
Sort(y, sa, rk);
for (int m = 1, p = 0; p < n; k = p, m <<= 1) {
for (p = 0; p < m; ++p) y[p + 1] = n - m + p + 1;
for (int i = 1; i <= n; ++i) if (sa[i] > m) y[++p] = sa[i] - m;
Sort(y, sa, rk), swap(rk, y);
rk[sa[p = 1]] = 1;
for (int i = 2; i <= n; ++i) rk[sa[i]] = cmp(y, sa[i], sa[i - 1], m) ? p : ++p;
}
for (int i = 1; i <= n; ++i) rnk[sa[i]] = i, Y[i] = 0;
}
void get_H() {
for (int i = 1, k = 0; i <= n; H[rnk[i++]] = k)
for (k ? --k : 0; s[i + k] == s[sa[rnk[i] - 1] + k]; ++k);
for (int i = 2; i <= n; ++i) st[i][0] = H[i];
for (int j = 1; j <= __lg(n); ++j)
for (int i = 2; i + (1 << j) - 1 <= n; ++i)
st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}
void clear() {
memset(rnk, 0, sizeof rnk);
}
int lcp(int x, int y) {//求后缀x和y的lcp
x = rnk[x], y = rnk[y];
if (x > y) swap(x, y);
++x;
int k = __lg(y - x + 1);
return min(st[x][k], st[y - (1 << k) + 1][k]);
}
};


posted @ 2019-03-25 15:15  QvvQ  阅读(213)  评论(1编辑  收藏