P2852 [USACO06DEC]牛奶模式Milk Patterns

Posted oi-forever

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2852 [USACO06DEC]牛奶模式Milk Patterns相关的知识,希望对你有一定的参考价值。

题目描述

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.

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

输入输出格式

输入格式:

 

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.

 

输出格式:

 

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

 

输入输出样例

输入样例#1: 复制
8 2
1
2
3
2
3
2
3
1
输出样例#1: 复制
4

总结:其实离散化后就和这题一样了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int n, w, wr[maxn], c[maxn], rk[maxn], b[maxn], a[maxn], y[maxn];
int p, m, ln, k, sa[maxn], H[maxn], pos[maxn], q[maxn], l, r;
void SA(int x) {
	m = x;
	for (int i = 1; i <= n; ++i) rk[i] = b[i];
	for (int i = 1; i <= n; ++i) c[rk[i]]++;
	for (int i = 1; i <= m; ++i) c[i] += c[i - 1];
	for (int i = n; i >= 1; --i) sa[c[rk[i]]--] = i;
	p = 0; ln = 1;
	while(p < n) {
		k = 0;
		for (int i = n - ln + 1; i <= n; ++i) y[++k] = i;
		for (int i = 1; i <= n; ++i) if(sa[i] > ln) y[++k] = sa[i] - ln;
		memset(c, 0, sizeof c);
		for (int i = 1; i <= n; ++i) wr[i] = rk[i];
		for (int i = 1; i <= n; ++i) c[wr[y[i]]]++;
		for (int i = 1; i <= m; ++i) c[i] += c[i - 1];
		for (int i = n; i >= 1; --i) sa[c[wr[y[i]]]--] = y[i];
		for (int i = 1; i <= n; ++i) wr[i] = rk[i];
		p = 1; rk[sa[1]] = 1;
		for (int i = 2; i <= n; ++i) {
			if(!(wr[sa[i]] == wr[sa[i - 1]] && wr[sa[i] + ln] == wr[sa[i - 1] + ln])) ++p;
			rk[sa[i]] = p;
		} m = p; ln <<= 1;
	}
}
void GH() {
	k = 0;
	for (int i = 1; i <= n; ++i) {
		if(k) --k; int j = sa[rk[i] - 1];
		while(b[i + k] == b[j + k]) ++k;
		H[rk[i]] = k;	
	}
}
int main() {
	scanf("%d%d", &n, &w);
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), b[i] = a[i];
	sort(a + 1, a + n + 1); int tot = 0;
	tot = unique(a + 1, a + n + 1) - a - 1;
	for (int i = 1; i <= n; ++i) b[i] = lower_bound(a + 1, a + tot + 1, b[i]) - a;
	SA(tot + 1); GH(); int ans = 0;
	for (int i = 1; i <= n; ++i) {
		while(l < r && H[i] < q[r]) --r;
		q[++r] = H[i]; pos[r] = i;
		while(l < r && pos[l] + w - 1 <= i) ++l;
		if(i >= w) ans = max(ans, q[l]);
	} printf("%d
", ans);
	return 0;
}

  

 

以上是关于P2852 [USACO06DEC]牛奶模式Milk Patterns的主要内容,如果未能解决你的问题,请参考以下文章

P2852 [USACO06DEC]Milk Patterns

Luogu2852 [USACO06DEC]牛奶模式

[Luogu2852][USACO06DEC]牛奶模式Milk Patterns

bzoj1717 [Usaco2006 Dec]Milk Patterns 产奶的模式

[bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式

洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing