Suffix array hash for LCP Bzoj 4310: Flea

Source: Internet
Author: User

The suffix array has not been put in the title Blog. So dug up a write to enrich the next blog by the way to stay as a Board.

A string s in which the contents of different substrings have sigma{n-sa[i]+1-h[i]} (oh H[here) is known Height[])

So l=1,r= the above Sigma dichotomy is the number of substrings in the dictionary sequence.

Then ask S in the K-large substring w: because h[i] is related to i-1, so we have to subtract from N Downto 1,k-=n-sa[i]+1-h[i] to K.

Apparently sweep the substring dictionary order is decremented so you can get the K large substring W

And then greedy n downto 1 if you encounter a substring larger than w, it is not feasible to verify the current dichotomy of this k-large

Judging than w big or small use hash two cent to find LCP can

1#include <bits/stdc++.h>2 #defineN 2000053 #defineLL Long Long4 using namespacestd;5 ConstLL mo=1000000007;6 intf[ -],a[n],rank[n],sa[n],h[n],g1[n],g2[n],next[n],n,m,ans,k,t,l,r,mid,x,y,z;7 Chars[n]; LL f[n],w[n];8 intOhintKintTintPintQ) {9     intL=0, R=min (t-k,q-p) +1, j;Ten      while(l<R) { onej=l+r+1>>1; a(f[k+j-1]-f[k-1]*w[j]%mo+mo)%mo== (f[p+j-1]-f[p-1]*w[j]%mo+mo)%mo? -l=j:r=j-1; -     } the     if(k+l>t)return 0; -     if(p+l>q)return 1; -     returna[k+l]>a[p+l]; - } + intJudintU) { -     intp,q,k,t; +      for(inti=n;i;--I) a         if(n-sa[i]+1-h[i]<u) u-=n-sa[i]+1-h[i]; at         Else{p=sa[i];q=n-u+1; break;} -t=n; k=1; -      for(intI=n;i;) -     if(oh (i,t,p,q)) { -         if(i==t)return 0; -t=i; ++k; in}Else--i; -     if(k>m)return 0;return 1; to } + intmain () { -scanf"%d", &m); scanf"%s", s+1); N=strlen (s+1); thew[0]=1; *      for(intI=1; i<=n;++I) { $a[i]=s[i]-'a'+1;Panax Notoginsengf[a[i]]=1; -f[i]= (f[i-1]* in+a[i])%mo; thew[i]=w[i-1]* in%mo; +     } a      for(intI=2; i<= -; ++i) f[i]+=f[i-1]; the      for(intI=1; I<=n;++i) rank[i]=f[a[i]]; t=f[ -]; +      for(intm=1; m<n;m<<=1){ -          for(intI=1; i<=n;++I) { $next[i]=g1[rank[i+m]]; $g1[rank[i+m]]=i; -         } -          for(inti=t;i>=0;--I) { the              for(intj=g1[i];j;j=k) { -k=next[j]; next[j]=g2[rank[j]]; g2[rank[j]]=j;Wuyi             } theg1[i]=0; -         } wuz=0; -          for(intI=1; i<=t;++I) { abouty=-1; $              for(intj=g2[i];j;j=k) { -k=next[j]; next[j]=0; -                 if(y!=rank[j+m]) y=rank[j+m],++z; -h[j]=z; a             } +g2[i]=0; the         } -t=z; $          for(intI=1; I<=n;++i) rank[i]=h[i]; the     } the      for(intI=1; I<=n;++i) sa[rank[i]]=i,h[i]=0; the      for(intI=1; i<=n;++I) the     if(rank[i]!=1){ -k=h[rank[i-1]];if(k)--k; in          while(a[i+k]==a[sa[rank[i]-1]+k]) + +k; theh[rank[i]]=k; r+=n-i+1-k; the     } aboutL=1; ++r; the      while(l<R) { thek=l+r+1>>1; theJud (k)? l=k:r=k-1; +     } -      for(inti=n;i;--I) the     if(n-sa[i]+1-h[i]<l) l-=n-sa[i]+1-h[i];Bayi     Else{k=sa[i];t=n-l+1; break;} the      for(intI=k;i<=t;++i) printf ("%c", a[i]+'a'-1); the     return 0; -}
View Code

Suffix array hash for LCP Bzoj 4310: Flea

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: 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.