题目链接:poj 3261 Milk Patterns
Milk Patterns
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 13092 Accepted: 5825
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 ≤ 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
AC代码:
题意:求重复k次的最长可重叠子串。
思路:二分,然后对H分组,看同一组里面是否有至少k个后缀。
//#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e4 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int cmp(int *r, int a, int b, int l) {
return (r[a] == r[b]) && (r[a+l] == r[b+l]);
}
int wa[MAXN], wb[MAXN], ws[MAXN], wv[MAXN];
int R[MAXN];//下标0->n-1 存储的是1->n之间的数
int H[MAXN];//下标2->n H[i] >= H[i-1]-1
void DA(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for(i = 0; i < m; i++) ws[i] = 0;
for(i = 0; i < n; i++) ws[x[i]=r[i]]++;
for(i = 1; i < m; i++) ws[i] += ws[i-1];
for(i = n-1; i >= 0; i--) sa[--ws[x[i]]] = i;
for(j = 1, p = 1; p < n; j *= 2, m = p)
{
for(p = 0, i = n-j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < n; i++) wv[i] = x[y[i]];
for(i = 0; i < m; i++) ws[i] = 0;
for(i = 0; i < n; i++) ws[wv[i]]++;
for(i = 1; i < m; i++) ws[i] += ws[i-1];
for(i = n-1; i >= 0; i--) sa[--ws[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;
}
}
void calh(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i = 1; i <= n; i++) R[sa[i]] = i;
for(i = 0; i < n; H[R[i++]] = k)
for(k ? k-- : 0, j = sa[R[i]-1]; r[i+k] == r[j+k]; k++);
}
int sa[MAXN];//下标从1->n,存储的是0->n-1之间的数
bool judge(int k, int n, int o) {
int cnt = 0; int ans = 0;
for(int i = 2; i <= n; i++) {
if(H[i] < k) {
cnt = 0;
}
else {
cnt++;
ans = max(ans, cnt + 1);
if(ans >= o) return true;
}
}
return false;
}
int a[MAXN];
int main()
{
int n, k;
while(scanf("%d%d", &n, &k) != EOF) {
int Max = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
Max = max(Max, a[i]);
}
a[n] = 0;
DA(a, sa, n+1, Max+1);//<n+1
calh(a, sa, n);//<=n
int ans = 0; int l = 0, r = n;
while(r >= l) {
int mid = (l + r) >> 1;
if(judge(mid, n, k)) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
printf("%d\n", ans);
}
return 0;
}