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