Suffix array Template/LCP template

Source: Internet
Author: User

1 //suffix array template, Manx size of the arrays2 //the supported operations have a computed suffix array (sa array), which computes the longest common prefix (height array) of the two adjacent elements, using get_height ();3 //calculate the longest common prefix for two suffixes A, and B, first use Lcp_init (), and then call GET_LCP (A, b) to get4 //The following n is the length of the input string +1 (n = strlen (s) + 1), and M is the range of the template m=128 expressed in letters, numbers within the range, can be enlarged or narrowed5 //S[len] is an inserted character that is smaller than the input character6 structSufarray {7     CharS[MAXN];8     intSA[MAXN], T[MAXN], T2[MAXN], C[MAXN], N, m;9     intRNK[MAXN], HEIGHT[MAXN];//rnk and Height arraysTen     intmi[maxn][ -], IDXK[MAXN];//for calculating LCP One  A     voidinit () { - Mem0 (s); - mem0 (height); the     } -     //read-in string as input -     voidRead_str () { - gets (s); +m = -; -n =strlen (s); +s[n++] =' '; A     } at     voidBuild_sa () { -         int*x = T, *y =T2; -Rep (I,0, M-1) C[i] =0; -Rep (I,0N1) C[x[i] = S[i]] + +; -Rep (I,1, M-1) C[i] + = c[i-1]; -Dec (i, N-1,0) Sa[--c[x[i]] =i; in          for(intK =1; K <= N; K <<=1) { -             intp =0; toRep (i, N-k, N-1) y[p++] =i; +Rep (I,0N1)if(Sa[i] >= k) y[p++] = Sa[i]-K; -Rep (I,0, M-1) C[i] =0; theRep (I,0N1) C[x[y[i]] + +; *Rep (I,0, M-1) C[i] + = c[i-1]; $Dec (i, N-1,0) sa[--c[x[y[i] []] =Y[i];Panax Notoginseng swap (x, y); -p =1; thex[sa[0]] =0; +Rep (I,1N1) { AX[sa[i]] = y[sa[i-1] [= Y[sa[i]] && y[sa[i-1] + K] = = Y[sa[i] + K]? P-1: p++; the             } +             if(P >= N) Break; -m =p; $         } $     } -     voidget_height () { -         intK =0; theRep (I,0N1) Rnk[sa[i]] =i; -Rep (I,0N1) {Wuyi             if(k) K--; the             intj = Sa[rnk[i]-1]; -              while(S[i + K] = = S[j + K]) K + +; WuHeight[rnk[i]] =K; -         } About     } $     voidRmq_init (int*a,intN) { -Rep (I,0N1) mi[i][0] =A[i]; -          for(intj =1; (1<< j) <= N; J + +) { -              for(inti =0; i + (1&LT;&LT;J)-1< n; i + +) { AMi[i][j] = min (Mi[i][j-1], Mi[i + (1<< (J-1))][j-1]); +             } the         } -Rep (Len,1, N) { $Idxk[len] =0; the              while((1<< (Idxk[len] +1)) <= len) Idxk[len] + +; the         } the     } the     intRmq_min (intLintr) { -         intLen = r-l +1, k =Idxk[len]; in         returnMin (Mi[l][k], Mi[r-(1<< k) +1][k]); the     } the     voidLcp_init () { About get_height (); the rmq_init (height, n); the     } the     intGET_LCP (intAintb) { +         if(A = = b)returnN-a-1; -         returnRmq_min (min (Rnk[a], rnk[b]) +1, Max (Rnk[a], rnk[b])); the     }Bayi     voidsolve () { the     } the};

Suffix array Template/LCP 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.