Milk Patterns
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 12128 |
|
Accepted: 5387 |
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 overn (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 leastK 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
Source
Usaco 2006 December Gold
AC Code
problem:3261 user:kxh1995 memory:680k time:47ms language:c++
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using Namespac e std; int s[20020]; int sa[20020],t1[20020],t2[20020],c[20020]; int rank[20020],height[20020]; void Build_sa (int s[],int n,int m) {int i,j,p,*x=t1,*y=t2; for (i=0;i<m;i++) c[i]=0; for (i=0;i<n;i++) c[x[i]=s[i]]++; for (i=1;i<m;i++) c[i]+=c[i-1]; for (i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for (j=1;j<=n;j<<=1) {p=0; for (i=n-j;i<n;i++) y[p++]=i; for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j; for (i=0;i<m;i++) c[i]=0; for (i=0;i<n;i++) c[x[y[i]]]++; for (i=1;i<m;i++) c[i]+=c[i-1]; for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; Swap (x, y); P=1; x[sa[0]]=0; for (i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++; if (p>=n) break; M=p; }} void GetHeight (int s[],int n) {int i,j,k=0; for (i=0;i<=n;i++) rank[sa[i]]=i; for (i=0;i<n;i++) {if (k) k--; J=SA[RANK[I]-1]; while (S[i+k]==s[j+k]) k++; Height[rank[i]]=k; }} int judge (int n,int k,int len) {int num=1;for (int i=1;i<=n;i++) {if (Height[i]>=len) {num++;if (num>=k) return 1 ;} Elsenum=1;} return 0;} int main () {int n,k;while (scanf ("%d%d", &n,&k)!=eof) {int i;int num=-1;for (i=0;i<n;i++) {scanf ("%d", &s[ I]); if (Num<s[i]) num=s[i];} S[n]=0;build_sa (s,n+1,num+1); getheight (s,n); int ans=0;int L=0,r=n;while (l<=r) {int mid= (L+R) >>1;if (judge ( N,k,mid)) {ans=mid;l=mid+1;} Elser=mid-1;} printf ("%d\n", ans);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ topic 3261 Milk Patterns (suffix array for longest overlap at least k-times substring length)