Suffix array template

Source: Internet
Author: User

Suffix array template, so the description is very detailed!


/* suffix Array template-multiplication method * Usage: * 1, read string converted to int array, length len, subscript starting from 0 * 2, add a dictionary order minimum character at the end of string, General 0, and find the largest word subscript character for Maxa * 3, call function Da (num,sa,len+1, maxa+1) * Meaning of the SA array: sa[i] is the first letter subscript of the dictionary suffix string for the I, call the function build_height (num,sa,len) * The meaning of the ranking array: ranking[i] The prefix of the suffix array, which is labeled I, is the position of the dictionary order of the "Height array" (meaning: Height[i] is the longest common prefix of the dictionary suffix and the i-1 dictionary suffix, height[1] = 0; *h[i]=height[rank[i]], which is the longest public prefix of suffix (i) and the suffix in its previous name. */#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] && Amp 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[r Anking[i]-1]; R[i + K] = = R[j + K]; k++);}} int Sa[maxn];int Main () {freopen ("in.in", "R", stdin), Freopen ("Out.out", "w", stdout); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Suffix array template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.