Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 13072   Accepted: 5812
Case Time Limit: 2000MS

Description

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has 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 may 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 that at least one subsequence is repeated at least Ktimes.

Input

Line 1: Two space-separated integers: N and K 
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4


又是一道瘠薄题
题目大意:求可重叠k次的最长子串长度,同poj1743,只是二分的时候改成如果同一组内元素个数大于等于K return 1
 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #define maxn 2000005
 7 int num[maxn],ws[maxn],wa[maxn],wb[maxn],sa[maxn];
 8 int rank[maxn],h[maxn],n,wv[maxn],K;
 9 bool cmp(int *r,int a,int b,int l){
10     return r[a]==r[b]&&r[a+l]==r[b+l];
11 }
12 void da(int *r,int *sa,int n,int m){
13     int *t,*x=wa,*y=wb,i,j,p;
14     for (i=0;i0;
15     for (i=0;ir[i];
16     for (i=0;i;
17     for (i=1;i1];
18     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
19     for (j=1,p=1;p2,m=p){
20         for (p=0,i=n-j;ii;
21         for (i=0;iif (sa[i]-j>=0) y[p++]=sa[i]-j;
22         for (i=0;i0;
23         for (i=0;ix[y[i]];
24         for (i=0;i;
25         for (i=1;i1];
26         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
27         for (t=x,x=y,y=t,i=1,p=1,x[sa[0]]=0;i)
28          x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
29     } 
30 }
31 void cal(int *r,int n){
32     int i,j,k=0;
33     for (int i=1;i<=n;i++) rank[sa[i]]=i;
34     for (int i=0;ik)
35      for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
36 }
37 bool check(int x){
38     int i=2,cnt;
39     while (1){
40         while (i<=n&&h[i];
41         if (i>n) break;
42         cnt=1;
43         while (i<=n&&h[i]>=x){
44             cnt++;
45             i++;
46         }
47         if (cnt>=K) return 1;
48     }
49     return 0;
50 }
51 void work(){
52     int l=1,r=n,ans;
53     while (l<=r){
54         int mid=(l+r)/2;
55         if (check(mid)) l=mid+1,ans=mid;
56         else r=mid-1;
57     }
58     printf("%d\n",ans);
59 }
60 int main(){
61     while (~scanf("%d%d",&n,&K)){
62           for (int i=0;i"%d",&num[i]);
63           for (int i=0;i;
64           num[n]=0;
65           da(num,sa,n+1,20005);
66           cal(num,n);    
67           work();
68     }
69 }