4
An array of "parse" suffixes
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespacestd;Const intN =20000+ -;intcmpint*r,intAintBintl) { return(R[a]==r[b]) && (r[a+l]==r[b+l]);}//used to compare the first keyword with the second keyword,//The special place is, when preprocessing, r[n]=0 (less than the preceding characters)/*DA (aa,sa,n+1,200); Calheight (aa,sa,n);*/intWa[n],wb[n],ws[n],wv[n];intRank[n];//The rank of suffix i in sa[]intHeight[n];//Sa[i] and Sa[i-1] LCPintSa[n];//Sa[i] Indicates the subscript for the small suffix of the rank ivoidDA (int*r,int*sa,intNintM//here n is more than 1 of the input n, a manually added character used to avoid CMP time out of bounds{ inti,j,p,*x=wa,*y=wb,*T; for(i=0; i<m; i++) ws[i]=0; for(i=0; i<n; i++) ws[x[i]=r[i]]++; for(i=1; i<m; i++) ws[i]+=ws[i-1]; for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i;//preprocessing length is 1 for(j=1, p=1; p<n; j*=2, m=p)//The SA that has been calculated for the length of J, to find 2*j SAS { for(p=0, I=n-j; i<n; i++) y[p++]=i;//Special handling without a second keyword for(i=0; i<n; i++)if(sa[i]>=j) Y[p++]=sa[i]-j;//using the length J, sort by the second keyword for(i=0; i<n; i++) wv[i]=X[y[i]]; for(i=0; i<m; i++) ws[i]=0; for(i=0; i<n; i++) ws[wv[i]]++; for(i=1; i<m; i++) ws[i]+=ws[i-1]; for(i=n-1; i>=0; i--) Sa[--ws[wv[i]]]=y[i];//Base Sort Section for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n; i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +;//update rank array x[], pay attention to the same }}voidCalheight (int*r,int*sa,intN//here n is the actual length{ inti,j,k=0;//the legal range of height[] is 1-n, where 0 is the end-added character for(i=1; i<=n; i++) rank[sa[i]]=i;//rank according to SA for(i=0; i<n; height[rank[i++]] = k)//definition: h[i] = height[Rank[i]] for(k?k--:0, j=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);//optimize the calculation height process according to H[i] >= h[i-1]-1}intn,m;CharSs[n];intAa[n];intSolveintk) { intans=0; for(intI=1; i<=n; i++) { if(height[i]<k)Continue; intMaxx=max (sa[i-1],sa[i]), Minn=min (sa[i-1],sa[i]); intcun=2; intj=i; while(height[j+1]>=k&&j<=n) j++,cun++; Ans=Max (Ans,cun); I=J; } returnans>=m;}intMain () {scanf ("%d%d",&n,&m); for(intI=0; i<n; i++) scanf ("%d",&Aa[i]); Aa[n]=0; DA (Aa,sa,n+1, -); Calheight (Aa,sa,n); intL=0, r=n,ans=-1;; while(l<=r) {intMid= (l+r) >>1; if(Solve (mid)) ans=mid,l=mid+1; Elser=mid-1; } printf ("%d\n", ans); return 0;}