[USACO06DEC]牛奶模式Milk Patterns
作者:互联网
Description
求字符串 \(s\) 的最长可重叠重复 \(k\) 次子串。
Solution
重复出现了 \(k\) 次,相当于我们选择了 \(k\) 个后缀,求他们的 \(lcp\)。
显然 \(k\) 个后缀的 \(rank\) 是连续的,所以重复出现 \(k\) 次的前缀就是 \(min(height[l+1\dots l+k-1])\)。
所以我们需枚举 \(i\),然后用一个递增的单调队列维护 \(height[i-k+2\dots i]\) 即可。
Code
#include <bits/stdc++.h>
#define rg register
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 _ = 2e4 + 10;
int N, K, s[_], ans = 0;
int 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 (rg int i = 1; i <= N; ++i) a[i] = lower_bound(t + 1, end, s[i]) - t;
fill(buc + 1, buc + N + 1, 0);
for (rg int i = 1; i <= N; ++i) ++buc[a[i]];
for (rg int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
for (rg int i = 1; i <= N; ++i) rnk[i] = buc[a[i] - 1] + 1;
for (rg int len = 1; len <= N; len <<= 1) {
for (rg 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 (rg int i = 1; i <= N; ++i) ++buc[sec[i]];
for (rg int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
for (rg int i = 1; i <= N; ++i) tmp[N - --buc[sec[i]]] = i;
fill(buc + 1, buc + N + 1, 0);
for (rg int i = 1; i <= N; ++i) ++buc[fir[i]];
for (rg int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
for (rg 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;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("milk.in", "r", stdin);
freopen("milk.out", "w", stdout);
#endif
N = ty(), K = ty();
for (rg int i = 1; i <= N; ++i) s[i] = ty();
SA();
/*for (int i = 1; i <= N; ++i) printf("%d ", height[i]);
puts("");*/
static int q[_], l, r;
q[l = r = 1] = 0;
for (int i = 1; i <= N; ++i) {
while (l < r && q[l] <= i - K + 1) ++l;
while (l < r && height[q[r - 1]] >= height[i]) --r;
q[r++] = i;
if (i >= K) ans = max(ans, height[q[l]]);
}
printf("%d\n", ans);
return 0;
}
标签:ch,USACO06DEC,int,height,Patterns,while,ans,buc,Milk 来源: https://www.cnblogs.com/newbielyx/p/12159560.html