"BZOJ1717" mode of milk production (suffix array)

Source: Internet
Author: User

Model of "BZOJ1717" milk (suffix array) surface

Permission questions
Hihocoder
Rokua

Exercises

\ (hihocoder\) It's very good.

This question requires the longest overlapping K second son string

The so-called same sub-string
We can understand that if there are two suffixes with the same prefix
Then there is the same substring

If the prefix of the two suffix is the same
So their rankings in \ (sa\) are close

Say it clearly.
If the prefix of the two suffix is the same
Must be the same prefix for a successive suffix in the suffix sort

Therefore, after finding the \ (height\) array
Consider how to calculate the answer:
It's obvious that you can't do it directly.
So two points to the answer

How \ (check\) is there a \ ( k\) repeating substring of length \ (mid\) ?
Since it's a continuous interval,
Therefore, it is necessary to check if there are more than \ ( k\) consecutive \ (height\) \ ( >=mid\)

This is easy to prove:
If you have \ (L.. r\) \ (height\) has exceeded \ (mid\)
So, the proof of any two suffixes in this interval
\ (lcp\) length is at least \ (mid\)
Therefore, this time \ (mid\) must appear more than \ (k\) times

So, vigorously two points can be

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <set>#include <map>#include <vector>#include <queue>using namespaceStd#define MAX 1200000inline intRead () {intx=0, t=1;CharCh=getchar (); while((ch<' 0 '|| Ch>' 9 ') &&ch!='-') Ch=getchar ();if(ch=='-') t=-1, Ch=getchar (); while(ch<=' 9 '&&ch>=' 0 ') x=x*Ten+ch-48, Ch=getchar ();returnX*t;}intSa[max],rank[max],x[max],y[max],t[max];intHeight[max],a[max];intN,k;BOOLcmpintIintJintK) {returnY[I]==Y[J]&AMP;&AMP;Y[I+K]==Y[J+K];}voidGetsa () {intm=1000010; for(intI=1; i<=n;++i) t[x[i]=a[i]]++; for(intI=1; i<=m;++i) T[i]+=t[i-1]; for(inti=n;i>=1; i.) sa[t[x[i]]--]=i; for(intk=1; k<=n;k<<=1)    {intp=0; for(intI=1; i<=n;++i) y[i]=0; for(intI=n-k+1; i<=n;++i) y[++p]=i; for(intI=1; i<=n;++i)if(sa[i]>k) Y[++p]=sa[i]-k; for(intI=0; i<=m;++i) t[i]=0; for(intI=1; i<=n;++i) t[x[y[i]]]++; for(intI=1; i<=m;++i) T[i]+=t[i-1]; for(inti=n;i>=1;----) sa[t[x[y[i]]]--]=y[i];        Swap (x, y); x[sa[1]]=p=1; for(intI=2; i<=n;++i) x[sa[i]]=cmp (sa[i],sa[i-1],k)? p:++p;if(p>=n) Break;    M=p; } for(intI=1; i<=n;++i) rank[sa[i]]=i; for(intI=1, j=0; i<=n;++i) {if(j) j--; while(A[i+j]==a[sa[rank[i]-1]+J]) J + +;    Height[rank[i]]=j; }}BOOLCheckinth) {intCnt=0; for(intI=2; i<=n;++i) {if(height[i]0;Elsecnt++;if(cnt==k-1)return true; }return false;}intMain () {n=read (); K=read (); for(intI=1; i<=n;++i) A[i]=read (); Getsa ();intL=1, r=n,ans=0; while(L&LT;=R) {intMid= (l+r) >>1;if(Check (mid)) Ans=mid,l=mid+1;ElseR=mid-1; } printf ("%d\n", ans);return 0;}

"BZOJ1717" mode of milk production (suffix array)

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.