"Suffix array" uoj#35. Suffix sort

Source: Internet
Author: User

Template

#include <cstdio> #include <algorithm> #include <cstring>using namespace std; #define N 100001int Sa[n] , T[n],t2[n],tong[n],n;char s[n];inline BOOL cmp (int *y,int i,int k) {return (Y[sa[i-1]]==y[sa[i]]) && ((sa[i-1]+ K>=n?-1:y[sa[i-1]+k]) = = (Sa[i]+k>=n?-1:y[sa[i]+k]);} Constructs a suffix array of string s, each character value must be 0~m-1, and the string is labeled 0~n-1 void Build_sa (int range) {int *x=t,*y=t2;//cardinality sort memset (tong,0,sizeof (int) * range);//Empty bucket for (int i=0;i<n;++i) tong[x[i]=s[i]]++;//copy S to x, then insert bucket for (int i=1;i<range;++i) tong[i]+=tong[i-1  ];//handles the bucket as a prefix and for (int i=n-1;i>=0;--i) sa[--tong[x[i]]]=i;for (int k=1;k<=n;k<<=1) {int p=0;  Use the SA array directly to sort the second keyword for (int i=n-k;i<n;++i) y[p++]=i;  for (int i=0;i<n;++i) if (sa[i]>=k) y[p++]=sa[i]-k; Cardinal Sort First Keyword memset (tong,0,sizeof (int) *range); for (int i=0;i<n;++i) tong[x[y[i]]]++;for (int i=0;i<range;++i) tong[i]+=tong[i-1];for (int i=n-1;i>=0;--i) sa[--tong[x[y[i]]]]=y[i];//calculates the new x array swap (x, y);p = 1 based on the SA and Y arrays; x[sa[0]]=0;for (int i=1;i<n;++i) x[sa[i]]= CMP (Y,I,K)?  P-1: P++;if (p>=n) break;range=p;  }}int rank[n],lcp[n];void GET_LCP () {int k=0;for (int i=0;i<n;++i) rank[sa[i]]=i;for (int i=0;i<n;++i) if (Rank[i])  {if (k)--k;  int j=sa[rank[i]-1];  while (S[i+k]==s[j+k]) ++k;  Lcp[rank[i]]=k; }}int Main () {scanf ("%s", s), N=strlen (s); Build_sa (' Z ' + 1); for (int i=0;i<n;++i) printf ("%d", sa[i]+1);p UTS (""); if (n >1) GET_LCP (); for (int i=1;i<n;++i) printf ("%d", Lcp[i]); return 0;}

"suffix array" uoj#35. Suffix sort

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.