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的最长子串的长度。
表示被后缀数组虐惨了,看懂倍增算法的代码都用了将近三个小时~~~这个题还写了一下午,各种错。
关于后缀数组还是去看NOI09年国家集训队的论文吧,讲得很详细了,就是读他给的示范代码略痛苦,不过读懂以后真觉得写的是简洁高效(不过好容易一不小心错的样子,还是不熟啊啊啊),自己也就那样写了……
这个就是在求出height 数组后二分判断有没有至少出现k 次的长度为x的重复子串。
——只要heighe数组中有>x,就有长度为x的可重复子串,然后再根据height分组:如果height[i]<x就把i和上一组分开,这样每一组里都存在长为x的重复字串,如果有任意一组里有超过k个,说明成立。
注意,有可能产奶量为0,所以将每个元素加一,然后再在串尾添加为0的最小元素表示结束才可以
#include <stdio.h>
#include <string>
const int N = 40000, M = 1000002;
int n, k, m;
int r[N];
int sa[N], rank[N], height[N];
int wa[N], wb[N], wv[N], ws[M];
int cmp(int *r,int a,int b,int l)
{
return ((r[a] == r[b]) && (r[a + l] == r[b + l]));
}
void calcsa(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];
t = x; x = y; y = t;
p = 1; x[sa[0]] = 0;
for (i = 1; i < n; i++)
{
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
//for (i = 1; i < n; i++)
//printf("%d ",y[i]);
//printf("\n");
}
return;
}
void calcheight(int *r, int *sa, int n)
{
long i, j, p = 0;
for(i = 1; i <= n; i++)
rank[sa[i]]=i;
for(i = 0; i < n; i++)
{
j = sa[rank[i] - 1];
while (r[i + p] == r[j + p])
{
p++;
}
height[rank[i]] = p;
if (p > 0) p--;
}
return;
}
bool check(long mid)
{
long i, s = 1;
for(i = 1; i <= n; i++)
{
if(height[i] >= mid)
{
s++;
if(s >= k) return 1;
}
else s = 1;
}
return 0;
}
void deal()
{
long l = 1, r = n;
while (l <= r)
{
long mid = (l + r) / 2;
if(check(mid)) l = mid + 1;
else r = mid - 1;
}
printf("%d\n", r);
return;
}
int main()
{
// freopen("poj3261.in", "r", stdin);
scanf("%d%d", &n, &k);
for (long i = 0; i < n; i++)
{
scanf("%d", &r[i]);
r[i]++;
}
r[n] = 0;
m = M;
calcsa(r, sa, n + 1, m);
calcheight(r, sa, n);
deal();
return 0;
}