Milk Patterns
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 12972 |
|
Accepted: 5769 |
Case Time Limit: 2000MS |
Description
Farmer John has noticed, the quality of milk given by he cows varies from day to day. On further investigation, he discovered so although he can ' t predict the quality of milk from one day to the next, there is some regular patterns in the daily milk quality.
To perform a rigorous study, he had invented a complex classification scheme by which each milk sample was recorded as an I Nteger between 0 and 1,000,000 inclusive, and have recorded data from a single cow over n (1≤ n ≤20,000 ) days. He wishes to find the longest pattern of samples which repeats identically at least K (2≤ k ≤ N) times. This could include overlapping patterns--1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.
Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed, at least one subsequence are repeated at least K times.
Input
Line 1:two space-separated integers:
Nand
K
Lines 2..
N+ 1:
NIntegers, one per line, the quality of the milk on day
IAppears on the
ITh line.
Output
Line 1:one Integer, the length of the longest pattern which occurs at least
KTimes
Sample Input
8 212323231
Sample Output
4
Idea: After using the suffix array to find the LCP, the 2-minute enumeration L makes the number of consecutive lcp[i]>=l >=k-1;
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=1000005;intBUF[MAXN];intSA[MAXN];intRNK[MAXN];intTMP[MAXN];intLCP[MAXN];intlen,k;intT;BOOLCompintIintj) { if(Rnk[i]!=rnk[j])returnrnk[i]<Rnk[j]; Else { intRi= (i+k<=len)? rnk[i+k]:-1; intrj= (j+k<=len)? rnk[j+k]:-1; returnri<RJ; }}voidGetsa () {memset (SA,0,sizeof(SA)); memset (RNK,0,sizeof(RNK)); memset (TMP,0,sizeof(TMP)); for(intI=0; i<len;i++) {Sa[i]=i; Rnk[i]=Buf[i]; } Sa[len]=Len; Rnk[len]=-1; for(k=1; k<=len;k*=2) {sort (Sa,sa+len+1, comp); tmp[sa[0]]=0; for(intI=1; i<=len;i++) {Tmp[sa[i]]=tmp[sa[i-1]]+ (Comp (sa[i-1],sa[i])?1:0); } for(intI=0; i<=len;i++) {Rnk[i]=Tmp[i]; } } }voidGETLCP () {Getsa (); memset (RNK,0,sizeof(RNK)); memset (LCP,0,sizeof(LCP)); for(intI=0; i<=len;i++) {Rnk[sa[i]]=i; } intH=0; lcp[0]=h; for(intI=0; i<len;i++) { intj=sa[rnk[i]-1]; if(h>0) h--; for(; i+h<len&&j+h<len;h++) { if(Buf[i+h]!=buf[j+h]) Break; } Lcp[rnk[i]-1]=h; } }voidDebug () { for(intI=0; i<=len;i++) { intL=Sa[i]; if(l==Len) {printf ("0\n"); } Else { for(intj=sa[i];j<len;j++) {printf ("%d", Buf[j]); } printf ("%d\n", Lcp[i]); } } }BOOLJudgeintl) { intCnt=0; for(intI=1; i<len;i++) { if(lcp[i]>=l)//to find a continuous length with a prefix greater than or equal to L{cnt++; } ElseCNT=0; if(cnt==t-1)return true; } return false;}voidsolve () {intL=1, r=Len; intans=0; while(l<=r) {intMid= (l+r) >>1; if(Judge (mid))//2-minute enumeration length{ans=Max (ANS,MID); L=mid+1; } Elser=mid-1; } printf ("%d\n", ans);}intMain () { while(SCANF ("%d%d", &len,&t)! =EOF) { for(intI=0; i<len;i++) scanf ("%d",&Buf[i]); GETLCP (); //Debug ()solve (); } return 0;}
POJ3261 (suffix array +2-minute enumeration)