Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 14045 | Accepted: 6220 | |
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
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
Sample Input
8 2 1 2 3 2 3 2 3 1
Sample Output
4
题意:给出一串数,求可重叠k次的最长重复子串。
也是09年论文上的,跟上一题是一起的,而且做法也差不多。
二分一个答案x,数出连续的大于等于x的height值的个数,如果大于等于k,x就可行,然后找出最大的x就好了,所以是O(nlogn)
嗯,就是这样的,虽然那个论文的解法我是没看懂。。。不过大神们的博客明显都是这个意思。。。。。。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int M = 1e6 + 10;
int w[M];
int rankk[M * 2];
int sa[M];
int height[M];
int s[M];
void get_sa_hei(int len)
{
static int m[M], tmp[M];
//int len = strlen(s + 1);
memset(rankk, 0, sizeof(int) * len * 2);
for (int i = 0; i <= 255; i++) w[i] = 0;
for (int i = 1; i <= len; i++) w[s[i]]++;
for (int i = 1; i <= 255; i++) w[i] += w[i - 1];
for (int i = len; i; i--) tmp[w[s[i]]--] = i;
rankk[tmp[1]] = 1;
for (int i = 2; i <= len; i++)
rankk[tmp[i]] = rankk[tmp[i-1]] + (s[tmp[i]] != s[tmp[i-1]]);
//get sa 用基数排序
for (int k = 1; k <= len; k <<= 1) {
for (int i = 0; i <= len; i++) w[i] = 0;
for (int i = 1; i <= len; i++) w[rankk[i+k]]++;
for (int i = 1; i <= len; i++) w[i] += w[i-1];
for (int i = len; i; i--) m[w[rankk[i+k]]--] = i;
for (int i = 0; i <= len; i++) w[i] = 0;
for (int i = 1; i <= len; i++) w[rankk[i]]++;
for (int i = 1; i <= len; i++) w[i] += w[i-1];
for (int i = len; i; i--) tmp[w[rankk[m[i]]]--] = m[i];
m[tmp[1]] = 1;
for (int i = 2; i <= len; i++)
m[tmp[i]] = m[tmp[i-1]] + (rankk[tmp[i]] != rankk[tmp[i-1]] || rankk[tmp[i]+k] != rankk[tmp[i-1]+k]);
for (int i = 1; i <= len; i++) rankk[i] = m[i];
}
for (int i = 1; i <= len; i++) sa[rankk[i]] = i;
//get height
for (int i = 1, j = 0; i <= len; i++) {
if (rankk[i] == 1) continue;
for (j ? j-- : 0; s[sa[rankk[i]-1]+j] == s[i+j]; j++);
height[rankk[i]] = j;
}
}
bool check(int n, int x, int k)
{
int ind = 1;
while (1) {
if (ind > n) break;
while (ind <= n && height[ind] < x) ind++;
int cascnt = 1;
while (ind <= n && height[ind] >= x) {
ind++;
cascnt++;
}
if (cascnt >= k) return true;
}
return false;
}
int main()
{
int n, m;
while (cin >> n >> m) {
for (int i = 1; i <= n; i++) {
scanf("%d", &s[i]);
}
get_sa_hei(n);
int ans = 0;
int l = 1, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(n, mid, m)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
printf("%d\n", ans);
}
return 0;
}