您的位置:首页 > 其它

poj 3261 Milk Patterns

2017-10-13 15:37 295 查看
Milk Patterns

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 16543 Accepted: 7309
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

题意:求出现k次的最长字串的长度
二分枚举长度,将连续且大于等于mid的height分组计数,如果超过不小于k-1个则满足。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxm = 1000005;
int Rank[maxm], s[maxm], sa[maxm], tp[maxm], a[maxm], fla[maxm], Height[maxm];
int n, m, rev;
int cmp(int *f, int x, int y, int w)
{
return f[x] == f[y] && f[x + w] == f[y + w];
}
void Rsort()
{
for (int i = 0;i <= m;i++) s[i] = 0;
for (int i = 1;i <= n;i++) s[Rank[tp[i]]]++;
for (int i = 1;i <= m;i++) s[i] += s[i - 1];
for (int i = n;i >= 1;i--) sa[s[Rank[tp[i]]]--] = tp[i];
}
void suffix()
{
for (int i = 1;i <= n;i++) Rank[i] = a[i], tp[i] = i;
Rsort();
for (int i, p = 1, w = 1;p < n;w += w, m = p)
{
for (p = 0, i = n - w + 1;i <= n;i++) tp[++p] = i;
for (i = 1;i <= n;i++) if (sa[i] > w) tp[++p] = sa[i] - w;
Rsort(), swap(Rank, tp), Rank[sa[1]] = p = 1;
for (i = 2;i <= n;i++) Rank[sa[i]] = cmp(tp, sa[i], sa[i - 1], w) ? p : ++p;
}
int j, k = 0;
for (int i = 1;i <= n;Height[Rank[i++]] = k)
for (k = k ? k - 1 : k, j = sa[Rank[i] - 1];a[i + k] == a[j + k];k++);
}
bool judge(int x)
{
int sum = 0;
for (int i = 1;i <= n;i++)
{
if (Height[i] >= x) sum++;
else
{
if (sum >= rev - 1) return 1;
sum = 0;
}
}
if (sum >= rev - 1) return 1;
return 0;
}
int main()
{
int i, j, k, sum;
scanf("%d%d", &n, &rev);
for (i = 1;i <= n;i++)
{
scanf("%d", &a[i]);
m = max(a[i], m);
}
suffix();
int l = 1, r = n + 1, ans;
while (r - l > 1)
{
int mid = (r + l) / 2;
if (judge(mid)) ans = mid, l = mid;
else r = mid;
}
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: