The template for the suffix array. That means it's very specific!
/* suffix Array template-multiplication method * Usage: * 1, read string converted to int array. Length is Len. Subscript starting from 0 * *, at the end of the string with a dictionary of the minimum character, generally 0, and find the largest word subscript character for Maxa * 3, call the function da (num,sa,len+1,maxa+1) * The meaning of the SA array: sa[i] for the first letter of the dictionary suffix string "i" , called the function build_height (num,sa,len) * The meaning of the ranking array: ranking[i] The suffix array of the first letter subscript I the dictionary order of the position of the index of the height array meaning: height[i] The longest common prefix for the dictionary suffix and the i-1 dictionary suffix, height[1] = 0; *h[i]=height[rank[i]]. The longest public prefix of suffix (i) and the suffix in its previous name. * NOTE: * The minimum value of a string converted to a number must be no less than 1. If it is 0 then the runtime Error, please self-weight ... * * #include <iostream> #include <algorithm> #include <cstdio> #define INF 2*0x3f3f3f3fusing namespace Std;const int maxn = 200001; Note the size of the array. Remember to change 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 for the obtained conversion string SA is evaluated for the SA array of length +1 m is the maximum value for the array +1int i, J, p, *x = WA, *y = WB, *t;f or (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] + = ws S[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 is the converted original array obtained, SA is an already evaluated array. N is Lenint 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;}
Suffix array template