POJ-3261-Milk Patterns (后缀数组)

Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17291   Accepted: 7649
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 ≤ KN) 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

Source

USACO 2006 December Gold

题意:求至少出现K次的最长可重叠字串
思路:后缀数组跑出height和sa,然后二分结果

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

const int MAXN = 20005;

int SA[MAXN], _rank[MAXN], Height[MAXN], tp[MAXN], a[MAXN], len, m, k;
int tax[1000004];
int str[MAXN];
///_rank[i] 第i个后缀的排名; SA[i] 排名为i的后缀位置; Height[i] 排名为i的后缀与排名为(i-1)的后缀的LCP
///tax[i] 计数排序辅助数组; tp[i] _rank的辅助数组(计数排序中的第二关键字),与SA意义一样。
///a为原串
void RSort() {

	for (int i = 0; i <= m; i++) tax[i] = 0;
	for (int i = 1; i <= len; i++) tax[_rank[tp[i]]] ++;
	for (int i = 1; i <= m; i++) tax[i] += tax[i - 1];
	for (int i = len; i >= 1; i--) SA[tax[_rank[tp[i]]] --] = tp[i];
}

int cmp(int *f, int x, int y, int w) { return (f[x] == f[y]) && (f[x + w] == f[y + w]); }

void Suffix() {
	for (int i = 1; i <= len; i++) _rank[i] = a[i], tp[i] = i;
	m = 20004, RSort();
	for (int w = 1, p = 1, i; p < len; w += w, m = p) {
		for (p = 0, i = len - w + 1; i <= len; i++) tp[++p] = i;
		for (i = 1; i <= len; i++) if (SA[i] > w) tp[++p] = SA[i] - w;
		RSort(), swap(_rank, tp), _rank[SA[1]] = p = 1;

		for (i = 2; i <= len; i++) _rank[SA[i]] = cmp(tp, SA[i], SA[i - 1], w) ? p : ++p;

	}
	for (int i = 1; i <= len; i++) _rank[SA[i]] = i;
	int j, k = 0;
	for (int i = 1; i <= len; Height[_rank[i++]] = k)
		for (k = k ? k - 1 : k, j = SA[_rank[i] - 1]; a[i + k] == a[j + k]; ++k);
}

bool check(int mid)
{
	int cnt = 0;
	for (int i = 2; i <= len; i++) {
		if (Height[i]<mid) cnt = 0;
		else {
			cnt++;
			if (cnt + 1 >= k) return true;
		}
	}
	return false;
}
int sol()
{
	int l = 1, r = len;
	int ans = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid)) l = mid + 1, ans = mid;
		else r = mid - 1;
	}
	return ans;
}
void Init() {
	for (int i = 1; i <= len; i++) a[i] = str[i];
}
int main()
{
	int n;
	while (~scanf("%d%d", &n, &k)) {
		for (int i = 1; i <= n; i++)
			scanf("%d", &str[i]);
		len = n;
		Init();
		Suffix();
		int ans = sol();
		printf("%d\n", ans);
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/79859433