Milk Patterns


Time Limit: 5000MS

 

Memory Limit: 65536K

Total Submissions: 14130

 

Accepted: 6263

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 K times.


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 次以上最大子串(可重叠)【单串】
二分枚举长度 len
连续height大于等于len的个数超过k个,说明长度为len的存在重复k次的子串

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 20005;
int s[maxn],t[maxn],t2[maxn];    
int Rank[maxn],height[maxn],sa[maxn];    
int buc[maxn];    

void getsa(int n,int m)    
{    
    int i,*x = t,*y = t2;    
    for(i = 0; i < m; i++) buc[i] = 0;    
    for(i = 0; i < n; i++) buc[x[i] = s[i]]++;    
    for(i = 0; i < m; i++) buc[i] += buc[i-1];    
    for(i = n-1; i >= 0; i--) sa[--buc[x[i]]] = i;    
    for(int k = 1; k <= n; k = k << 1)    
    {    
        int p = 0;    
        for(i = n-k; i < n; i++) y[p++] = i;    
        for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;    
        for(i = 0; i < m; i++) buc[i] = 0;    
        for(i = 0; i < n; i++) buc[x[y[i]]]++;    
        for(i = 0; i < m; i++) buc[i] += buc[i-1];    
        for(i = n-1; i >= 0; i--) sa[--buc[x[y[i]]]] = y[i];    
        std::swap(x,y);    
        p = 1, x[sa[0]] = 0;    
        for(i = 1; i < n; i++)    
            x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1:p++;    
        if(p >= n) break;    
        m = p;    
    }    
}    
    
void getheight(int n)    
{    
    int i,j,k = 0;    
    for(i = 1; i <= n; i++) Rank[sa[i]] = i;    
    for(i = 0; i < n; i++)    
    {    
        if(k) k--;    
        if(Rank[i] == 0)    
        {    
            height[Rank[i]] = 0;    
            continue;    
        }    
        j = sa[Rank[i]-1];    
        while(s[i+k] == s[j+k]) k++;    
        height[Rank[i]] = k;    
    }    
}  

bool check(int n,int k,int len)
{
	int cnt = 0;
	for(int i = 1; i <= n; i++)
	{
		if(height[i] < len) cnt = 0;
		else
		{
			cnt++;
			if(cnt == k - 1) return true;
		}
	}
	return false;
}

int solve(int n,int k)
{
	int l = 1,r = n,mid,ans;
	while(l <= r)
	{
		mid = (l + r) >> 1;
		if(check(n,k,mid))
		{
			ans = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return ans;
}

int main()
{
	int n,k;
	while(scanf("%d %d",&n,&k)!=EOF)
	{
		for(int i = 0; i < n; i++)
			scanf("%d",&s[i]);
		s[n] = 0;
		getsa(n+1,20005);
		getheight(n);
		printf("%d\n",solve(n,k));
	}
	return 0;
}