Suffix array template

Source: Internet
Author: User

1 ///suffix array template2 Sa[i] means that all suffixes are sorted by dictionary number to s[i]3The beginning suffix is ranked in the first position. Height[i] indicates that the number of dictionaries is I and I-1 suffix of4 The longest string of prefixes. 5 intS[mx],sa[mx],t[mx],t2[mx],c[mx],n;6 intRANK[MX],HEIGHT[MX];7 8 voidBuild_sa (intm)9 {Ten     inti,*x=t,*y=T2; One      for(i=0; i<m;i++) c[i]=0; A      for(i=0; i<n;i++) c[x[i]=s[i]]++; -      for(i=1; i<m;i++) c[i]+=c[i-1]; -      for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i; the      for(intk=1; k<=n;k<<=1) -     { -         intp=0; -          for(i=n-k;i<n;i++) y[p++]=i; +          for(i=0; i<n;i++)if(sa[i]>=k) y[p++]=sa[i]-K; -          for(i=0; i<m;i++) c[i]=0; +          for(i=0; i<n;i++) c[x[y[i]]]++; A          for(i=1; i<m;i++) c[i]+=c[i-1]; at          for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=Y[i]; - swap (x, y); -p=1; -x[sa[0]]=0; -          for(i=1; i<n;i++) -x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p + +; in         if(p>=n) Break; -m=p; to     } + } -  the voidgetheight () * { $     inti,j,k=0;Panax Notoginseng      for(i=0; i<n;i++) rank[sa[i]]=i; -      for(i=0; i<n;i++) the     { +         if(k) k--; A         intj=sa[rank[i]-1]; the          while(S[i+k]==s[j+k]) k++; +height[rank[i]]=K; -     } $}

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.