後綴數組模板
後綴數組的模板。這樣說明就非常具體了吧!
/* * 後綴數組模板-倍增法 * 用法: * 1、讀取字符串轉換成int數組。長度為len。下標從0開始 * 2、在字符串末尾加一字典序最小字符,一般為0,並找到最大的字符設為maxa * 3、調用函數da(num,sa,len+1,maxa+1) * 求得的sa數組的含義: sa[i]為第i字典序後綴字符串的首字母下標 * 4、調用函數build_height(num,sa,len) * 求得的ranking數組的含義: ranking[i]為首字母下標為i的後綴數組的字典序的名次 * 求得的height數組含義: height[i]為第i字典序後綴與第i-1字典序後綴的最長公共前綴,height[1] = 0; * h[i]=height[rank[i]]。也就是suffix(i)和在它前一名的後綴的最長公共前綴。* 註意事項: * 轉化為數字的字符串最小數值必須不小於1。假設是0的話會Runtime Error,請自重....... */ #include <iostream> #include <algorithm> #include <cstdio> #define INF 2*0x3f3f3f3f using namespace std; const int maxn = 200001; //註意數組的大小。記得更改 int wa[maxn], wb[maxn], wv[maxn], wss[maxn]; int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } void da(int *r, int *sa, int n, int m) { //r為所求取的轉化字符串 sa為求得的sa數組 n為數組長度+1 m為數組的最大值+1 int i, j, p, *x = wa, *y = wb, *t; for (i = 0; i < m; i++) wss[i] = 0; for (i = 0; i < n; i++) wss[x[i] = r[i]]++; for (i = 1; i < m; i++) wss[i] += wss[i - 1]; for (i = n - 1; i >= 0; i--) sa[--wss[x[i]]] = i; for (j = 1, p = 1; p < n; j *= 2, m = p) { for (p = 0, i = n - j; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j; for (i = 0; i < n; i++) wv[i] = x[y[i]]; for (i = 0; i < m; i++) wss[i] = 0; for (i = 0; i < n; i++) wss[wv[i]]++; for (i = 1; i < m; i++) wss[i] += wss[i - 1]; for (i = n - 1; i >= 0; i--) sa[--wss[wv[i]]] = y[i]; for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ?
p - 1 : p++; } return; } int ranking[maxn], height[maxn]; void build_height(int *r, int *sa, int n) { //r為求得的轉化原始數組,sa為已經求得的數組。n為len int i, j, k = 0; for (i = 1; i <= n; i++) ranking[sa[i]] = i; for (i = 0; i < n; height[ranking[i++]] = k) { for (k ? k-- : 0, j = sa[ranking[i] - 1]; r[i + k] == r[j + k]; k++); } } int sa[maxn], r[maxn]; int main() { freopen("in.in", "r", stdin); freopen("out.out", "w", stdout); return 0; }
後綴數組模板