POJ 3261 Milk Patterns

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


后缀数组+二分

#include<iostream>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Min = 0;
const int Max = 0x7FFFFFFF;
const int maxn = 20005;
const int bit = 1000005;
class suffix
{
private:
	int s[maxn];
	int r[maxn], w[bit], ss[maxn], h[maxn];
	int sa[maxn], rk[maxn + maxn], size;
	int limit;
public:
	bool get(){
		if (scanf("%d%d", &size,&limit) != 2)return false;
		for (int i = 1; i <= size; i++) scanf("%d", &s[i]);
		return true;
	}
	void pre()
	{
		memset(rk, 0, sizeof(rk));
		for (int i = 1; i <= bit; i++) w[i] = 0;
		for (int i = 1; i <= size; i++) w[(int)s[i]]++;
		for (int i = 1; i <= bit; i++) w[i] += w[i - 1];
		for (int i = size; i; i--) sa[w[(int)s[i]]--] = i;
		for (int i = 1, j = 1; i <= size; i++)
			rk[sa[i]] = (s[sa[i]] == s[sa[i + 1]] ? j : j++);
		for (int j = 1; j < size; j += j)
		{
			for (int i = 1; i <= size; i++) w[i] = 0;
			for (int i = 1; i <= size; i++) w[rk[i + j]]++;
			for (int i = 1; i <= size; i++) w[i] += w[i - 1];
			for (int i = size; i; i--) ss[w[rk[i + j]]--] = i;

			for (int i = 1; i <= size; i++) w[i] = 0;
			for (int i = 1; i <= size; i++) w[rk[ss[i]]]++;
			for (int i = 1; i <= size; i++) w[i] += w[i - 1];
			for (int i = size; i; i--) sa[w[rk[ss[i]]]--] = ss[i];

			for (int i = 1, k = 1; i <= size; i++)
				r[sa[i]] = (rk[sa[i]] == rk[sa[i + 1]] && rk[sa[i] + j] == rk[sa[i + 1] + j]) ? k : k++;
			for (int i = 1; i <= size; i++) rk[i] = r[i];
		}
		for (int i = 1, k = 0, j; i <= size; h[rk[i++]] = k)
		for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++);
	}
	void work()
	{
		int mid, l = 0, r = size >> 1;
		for (mid = (l + r) >> 1; l < r;)
		{
			int f = 0, sum = 1;
			for (int i = 2; i <= size; i++)
			{
				if (h[i] < mid) sum = 1;
				else
				{
					sum++;
					if (sum >= limit)
					{
						f = 1;
						break;
					}
				}
			}
			if (f) l = mid; else r = mid - 1;
			mid = (l + r) >> 1;
			if (l + 1 == r) mid++;
		}
		printf("%d\n", mid);
	}
}f;

int main()
{
	while (f.get())
	{
		f.pre();
		f.work();
	}
	return 0;
}


你可能感兴趣的:(poj)