后缀数组(仅模板,具体准备区域赛打完学)
后缀数组指的是讲某个字符串的所有后缀按字典序排序后得到的数组。
我们用sa[i]表示在字符串里排行第i个的字符串(按升序排序,及从小到大)是从第sa[i]个开始。
用rk[i]表示第i位开始的后缀在所有后缀里面排第几。
用lcp[i]表示从sa[i]开始的后缀和从sa[i + 1]开始的后缀的最长公共前缀。
有了以上三个数组我门就可以来处理好多好多的东西。
附上模板:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 80000 + 5; int wa[N],wb[N],ws[N],wv[N]; bool 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){ int *x = wa, *y = wb; for(int i = 0; i < m; i++) ws[i] = 0; for(int i = 0; i < n; i++) ws[x[i]=r[i]]++; for(int i = 1; i < m; i++) ws[i] += ws[i-1]; for(int i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; for(int j = 1, p = 1;p < n; j <<= 1, m = p) { p = 0; for(int i = n - j; i < n; i++) y[p++] = i; for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j; for(int i = 0; i < n; i++) wv[i] = x[y[i]]; for(int i = 0; i < m; i++) ws[i] = 0; for(int i = 0; i < n; i++) ws[wv[i]]++; for(int i = 1; i < m; i++) ws[i] += ws[i-1]; for(int i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; int *t = x; x = y; y = t; p = 1; x[sa[0]] = 0; for(int i = 1; i < n; i++) x[sa[i]] = cmp(y,sa[i-1],sa[i],j) ? p-1 : p++; } } void getlcp(int *r, int *sa, int *rk, int *lcp, int n) { for(int i = 1;i <= n; i++) rk[sa[i]] = i; int h = 0; lcp[0] = 0; lcp[n] = -1; for(int i = 0; i < n; i ++){ int j = sa[rk[i] - 1]; if(h > 0) h -= 1; for(; j + h < n && i + h < n; h ++) if(r[i + h] != r[j + h]) break; lcp[rk[i] - 1] = h; } } int n, a[N], rk[N], sa[N], lcp[N]; char s[N]; void solve() { int tot = 0; int n = strlen(s); for(int i = 0; i < n; i++) a[tot++] = s[i]-'0' + 1; a[tot ++] = 0; da(a, sa, tot, 128); getlcp(a, sa, rk, lcp, tot-1); for(int i = 0; i < tot; i ++) printf("sa[%d] = %d\n", i, sa[i]); for(int i = 0; i < tot; i ++) printf("rk[%d] = %d\n", i, rk[i]); for(int i = 0; i < tot; i ++) printf("lcp[%d] = %d\n", i, lcp[i]); } int main(){ while(scanf("%s", s) == 1)solve(); return 0; }
附上一题:hdu5442
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100000 + 5; char s[N], s1[N], s2[N]; int sa[N], rk[N], lcp[N], tmp[N], c[N], t[N], t2[N]; int mn[N][20]; int n, m, le; void build_sa(char *s, int *sa, int l, int m){ int *x = t, *y = t2; //基数排序 for(int i = 0; i < m; i ++) c[i] = 0; for(int i = 0; i < l; i ++) c[x[i] = s[i]] += 1; for(int i = 1; i < m; i ++) c[i] += c[i - 1]; for(int i = l - 1; i >= 0; i --) sa[--c[x[i]]] = i; for(int k = 1; k <= l; k <<= 1){ int p = 0; //直接利用sa数组排序第二关键字 for(int i = l - k; i < l; i ++) y[p ++] = i; for(int i = 0; i < l; i ++) if(sa[i] >= k) y[p ++] = sa[i] - k; //基数排序第一关键字 for(int i = 0; i < m; i ++) c[i] = 0; for(int i = 0; i < l; i ++) c[x[y[i]]] += 1; for(int i = 1; i < m; i ++) c[i] += c[i - 1]; for(int i = l - 1; i >= 0; i --) sa[--c[x[y[i]]]] = y[i]; //根据sa和y数组计算新的x数组 swap(x, y); p = 1; x[sa[0]] = 0; for(int i = 1; i < l; i ++) x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p - 1 : p ++; if(p >= l) break; m = p; } } void build_lcp(char *s, int *sa, int *lcp, int l){ for(int i = 0; i <= l; i ++) rk[sa[i]] = i; int h = 0; lcp[0] = 0; for(int i = 0; i < l; i ++){ int j = sa[rk[i] - 1]; if(h > 0) h -= 1; for(;j + h < l && i + h < l; h ++) if(s[i + h] != s[j + h]) break; lcp[rk[i] - 1] = h; } } struct RMQ{ int log2[N]; void init(int *h, int l){ for(int i = 0; i <= l; i ++) log2[i] = (i == 0 ? -1 : log2[i >> 1] + 1); for(int i = 0; i < l; i ++) mn[i][0] = h[i]; for(int j = 1; j < 20; j ++) for(int i = 1; i + (1 << j) <= l + 1; i ++) mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]); } int query(int ql, int qr){ int k = log2[qr - ql + 1]; return min(mn[ql][k], mn[qr - (1 << k) + 1][k]); } }rmq; int main(){ int T; scanf("%d", &T); while(T--){ scanf("%d", &le); scanf("%s", s); memset(s1, 0, sizeof(s1)); memset(s2, 0, sizeof(s2)); for(int i = 0; i < le; i ++)s[i + le] = s[i]; s[2 * le] = '\0'; le *= 2; build_sa(s, sa, le + 1, 128); int ans1 = sa[le] + 1; //printf("ans1 = %d\n", ans1); for(int i = 0; i < le / 2; i ++)s1[i] = s[ans1 + i - 1]; s1[le/2] = '\0'; reverse(s, s + le); build_sa(s, sa, le + 1, 128); build_lcp(s, sa, lcp, le); /* for(int i = 0; i <= le; i ++)printf("sa[%d] = %d\n", i, sa[i]); for(int i = 0; i <= le; i ++)printf("rk[%d] = %d\n", i, rk[i]); for(int i = 0; i <= le; i ++)printf("lcp[%d] = %d\n", i, lcp[i]); */ int ans2 = sa[le]; int st = le; //printf("le = %d\n", le); for(int i = le - 1; i >= 0; i --){ st =min(st, lcp[i]); //printf("sa[%d] = %d\n", i, sa[i]); if(st < le / 2) break; if(sa[i] < le / 2)ans2 = max(sa[i], ans2); } for(int i = 0; i < le / 2; i ++)s2[i] = s[ans2 + i]; ans2 = le/2 - ans2; s2[le/2] = '\0'; //puts(s1), puts(s2); int flag = strcmp(s1, s2); if(flag > 0) printf("%d 0\n", ans1); if(flag < 0) printf("%d 1\n", ans2); if(!flag){ if(ans1 <= ans2) printf("%d 0\n", ans1); else printf("%d 1\n", ans2); } } return 0; }
后缀数组还可以处理最长公共子串:传送门。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x7fffffff; const int N = 200000 + 5; int wa[N],wb[N],ws[N],wv[N]; int n, a[N], rk[N], sa[N], lcp[N]; char s[N], t[N]; bool 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){ int *x = wa, *y = wb; for(int i = 0; i < m; i++) ws[i] = 0; for(int i = 0; i < n; i++) ws[x[i]=r[i]]++; for(int i = 1; i < m; i++) ws[i] += ws[i-1]; for(int i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; for(int j = 1, p = 1;p < n; j <<= 1, m = p) { p = 0; for(int i = n - j; i < n; i++) y[p++] = i; for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j; for(int i = 0; i < n; i++) wv[i] = x[y[i]]; for(int i = 0; i < m; i++) ws[i] = 0; for(int i = 0; i < n; i++) ws[wv[i]]++; for(int i = 1; i < m; i++) ws[i] += ws[i-1]; for(int i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; int *t = x; x = y; y = t; p = 1; x[sa[0]] = 0; for(int i = 1; i < n; i++) x[sa[i]] = cmp(y,sa[i-1],sa[i],j) ? p-1 : p++; } } void getlcp(int *r, int *sa, int *rk, int *lcp, int n) { for(int i = 1;i <= n; i++) rk[sa[i]] = i; int h = 0; lcp[0] = 0; lcp[n] = -1; for(int i = 0; i < n; i ++){ int j = sa[rk[i] - 1]; if(h > 0) h -= 1; for(; j + h < n && i + h < n; h ++) if(r[i + h] != r[j + h]) break; lcp[rk[i] - 1] = h; } } int main(){ while(scanf("%s%s", s, t) == 2){ int ls = strlen(s); int lt = strlen(t); int l = 0; for(int i = 0; i < ls; i ++)a[l++] = s[i] - 'a' + 1; a[l++] = 27; for(int i = 0; i < lt; i ++)a[l++] = t[i] - 'a' + 1; a[l++] = 0; da(a, sa, l, 32); getlcp(a, sa, rk, lcp, l - 1); int ans = 0; for(int i = 0; i < l; i ++) if((sa[i] < ls) != (sa[i + 1] < ls))ans = max(ans, lcp[i]); printf("%d\n", ans); } return 0; }
既然要做,那就好好做!
自己选的路,自己走完!