[USACO5.1]乐曲主题Musical Themes - 后缀数组

Description

求字符串的最长不可重叠重复子串。

Solution

如果有两个子串相同,那么也就是有两个后缀的 \(lcp\) 相同。

所以考虑二分答案 \(K\),如果有连续一段的 \(height\) 都不小于 \(K\),那么这一段区间内,两两后缀的 \(lcp\) 都不小于 \(K\),那么记录一下区间的 \(\max\{sa_i\}\)\(\min\{sa_i\}\),如果 \(\max\{sa_i\}-\min\{sa_i\}\ge K\),那么就说明两个子串不重叠。

另外这题有一个“转调”的问题,只要差分一下就可以解决。不过需要注意的是,差分之后二分时的判断条件应该是 \(\max\{sa_i\}-\min\{sa_i\}> K\)

Code

#include <bits/stdc++.h>
using namespace std;

inline int ty() {
	char ch = getchar(); int x = 0, f = 1;
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}

const int _ = 5000 + 10;
const int INF = 0x3f3f3f3f;
int N, s[_], rnk[_], sa[_], height[_];

void SA() {
	static int t[_], a[_], buc[_], fir[_], sec[_], tmp[_];
	copy(s + 1, s + N + 1, t + 1);
	sort(t + 1, t + N + 1);
	int *end = unique(t + 1, t + N + 1);
	for (int i = 1; i <= N; ++i) a[i] = lower_bound(t + 1, end, s[i]) - t;
	for (int i = 1; i <= N; ++i) ++buc[a[i]];
	for (int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
	for (int i = 1; i <= N; ++i) rnk[i] = buc[a[i] - 1] + 1;
	for (int len = 1; len <= N; len <<= 1) {
		for (int i = 1; i <= N; ++i) {
			fir[i] = rnk[i];
			sec[i] = i + len > N ? 0 : rnk[i + len];
		}
		fill(buc + 1, buc + N + 1, 0);
		for (int i = 1; i <= N; ++i) ++buc[sec[i]];
		for (int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
		for (int i = 1; i <= N; ++i) tmp[N - --buc[sec[i]]] = i;
		fill(buc + 1, buc + N + 1, 0);
		for (int i = 1; i <= N; ++i) ++buc[fir[i]];
		for (int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
		for (int i, j = 1; j <= N; ++j) {
			i = tmp[j];
			sa[buc[fir[i]]--] = i;
		}
		bool same = false;
		for (int i, j = 1, last = 0; j <= N; ++j) {
			i = sa[j];
			if (!last) rnk[i] = 1;
			else if (fir[i] == fir[last] && sec[i] == sec[last]) rnk[i] = rnk[last], same = true;
			else rnk[i] = rnk[last] + 1;
			last = i;
		}
		if (!same) break;
	}
	for (int i = 1, k = 0; i <= N; ++i) {
		if (rnk[i] == 1) k = 0;
		else {
			if (k > 0) --k;
			int j = sa[rnk[i] - 1];
			while (i + k <= N && j + k <= N && a[i + k] == a[j + k]) ++k;
		}
		height[rnk[i]] = k;
	}
}

bool check(int mid) {
	int minsa = INF, maxsa = -1;
	for (int i = 1; i <= N; ++i) {
		if (height[i] < mid) minsa = maxsa = sa[i];
		else {
			minsa = min(minsa, sa[i]);
			maxsa = max(maxsa, sa[i]);
			if (maxsa - minsa > mid) return true;
		}
	}
 return false;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("theme.in", "r", stdin);
	freopen("theme.out", "w", stdout);
#endif
	N = ty();
	for (int i = 1; i <= N; ++i) s[i] = ty();
	for (int i = 1; i <= N; ++i) s[i] = s[i + 1] - s[i] + 100;
	SA();
	// for (int i = 1; i <= N; ++i) printf("%d ", height[i]);
	int l = 0, r = N;
	while (l < r) {
		int mid = (l + r + 1) >> 1;
		if (check(mid)) l = mid;
		else r = mid - 1;
	}
	printf("%d\n", l >= 4 ? l + 1 : 0);
	return 0;
}
posted @ 2020-01-07 10:36  newbielyx  阅读(176)  评论(0编辑  收藏  举报