1717: [Usaco2006 dec]milk Patterns Milk-producing mode time limit:5 Sec Memory limit:64 MB
submit:1017 solved:561
[Submit] [Status] [Discuss] Description
Farmer John found that the quality of his cow's milk had been changing. After careful investigation, he found that although he could not foresee the quality of milk produced tomorrow, there was a lot of overlap in the quality of several consecutive days. We call it a "pattern". John's milk by mass can be given a number between 0 and 1000000. And John recorded the milk quality value for N (1<=n<=20000) days. He wanted to know the length of the longest pattern that appeared at least K (2<=k<=n) times. For example 1 2 3 2 3 2 3 1 medium 2 3 2 3 appeared two times. When k=2, this length is 4.
Input
* Line 1: two integer n,k.
* Lines 2..n+1: An integer per line represents the mass value of the day.
Output
* Line 1: An integer: The length of the longest occurrence of a pattern of at least k times in n days
Sample Input8 2
1
2
3
2
3
2
3
1
Sample Output4Source
Gold
Overlapping K-times longest repeating substring
Two points to the longest degree of mid, and then the suffix sorts the results of the SA group, a group of Height>=mid, if there is a group in the number >=k so the length of mid is feasible
This is a common technique: grouping by the LCP (height) of the sorted suffix
The elements in the array can be 0, so that getheight always feel a bug, need to judge I+k<=n and so on
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intn=2e4+5, m=1e6+5; inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,m,k;intS[n];intSa[n],c[m],t1[n],t2[n];inlineBOOLcmpint*r,intAintBintj) { returna+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];}intRnk[n],height[n];voidgetheight () {intk=0; for(intI=1; i<=n;i++) rnk[sa[i]]=i; for(intI=1; i<=n;i++){ //if (rnk[i]==1) continue; if(k) k--; intj=sa[rnk[i]-1]; while(S[i+k]==s[j+k]) k++; Height[rnk[i]]=K; }}voidBuildsa () {int*r=t1,*k=T2; for(intI=1; i<=n;i++) c[r[i]=s[i]]++; for(intI=1; i<=m;i++) c[i]+=c[i-1]; for(inti=n;i>=1; i--) sa[c[r[i]]--]=i; for(intj=1; j<=n;j<<=1){//printf ("Hij%d\n", j); intp=0; for(inti=n-j+1; i<=n;i++) k[++p]=i; for(intI=1; i<=n;i++)if(SA[I]>J) k[++p]=sa[i]-J; for(intI=0; i<=m;i++) c[i]=0; for(intI=1; i<=n;i++) c[r[k[i]]]++; for(intI=1; i<=m;i++) c[i]+=c[i-1]; for(inti=n;i>=1; i--) sa[c[r[k[i]]]--]=K[i]; Swap (r,k);p=0; r[sa[1]]=++p; for(intI=2; i<=n;i++) r[sa[i]]=cmp (k,sa[i],sa[i-1],J)? p:++p; if(p>=n) Break; m=p; } getheight ();}BOOLCheckintmid) { intCnt=1; for(intI=2; i<=n;i++){ if(height[i]>=mid) {CNT++; if(cnt>=k)return true; }ElseCnt=1; } return false;}voidsolve () {intL=1, r=n,ans=0; while(l<=R) { intMid= (l+r) >>1; if(Check (mid)) l=mid+1, ans=mid; Elser=mid-1; } printf ("%d", ans);}intMain () {//freopen ("In.txt", "R", stdin);N=read (); k=read (); for(intI=1; i<=n;i++) S[i]=read (), M=max (M, (int) s[i]); BUILDSA (); Solve ();}
Bzoj 1717: [Usaco2006 dec]milk Patterns model of milk production [suffix array]