POJ - 3261 Milk Patterns (后缀数组 最长可重叠重复k次子串 + 二分)

本文介绍了一个算法挑战,即找出一个整数序列中最长的可重叠重复子序列,该子序列至少出现K次。通过使用复杂的分类方案和二分搜索策略,文章详细解释了如何高效地解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Milk Patterns

 

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 Ktimes.

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

题目链接:http://poj.org/problem?id=3261

题目大意:n k  ,给出长度为n的序列,问最长的可重叠的重复k次的子序列

思路:二分最长重复k次子序列,在连续区间[ l , r ] 中 hi>=x,且cnt>=k 就说明存在长度为x的重复k次子序列

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=200005;
int sa[N],rak[N],h[N],tax[N],tp[N],a[N],n,m;
int s[N];
bool 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++) tax[i]=0;
    for(int i=1;i<=n;i++) tax[rak[i]]++;
    for(int i=1;i<=m;i++) tax[i]+=tax[i-1];
    for(int i=n;i>=1;i--) sa[tax[rak[tp[i]]]--]=tp[i];
}
void suffix()
{
    for(int i=1;i<=n;i++) rak[i]=a[i],tp[i]=i;
    Rsort();
    for(int w=1,p=0,i;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(rak,tp); rak[sa[1]]=p=1;
        for(i=2;i<=n;i++) rak[sa[i]]=cmp(tp,sa[i],sa[i-1],w)?p:++p;
    }

    int j,k=0;
    for(int i=1;i<=n;h[rak[i++]]=k)
        for(k=k?k-1:k,j=sa[rak[i]-1];a[i+k]==a[j+k];++k);
}
int fun(int x,int k)
{
    int cnt=1;
    for(int i=2;i<=n;i++)
    {
        if(h[i]>=x)
        {
            cnt++;
            continue;
        }
        if(cnt>=k) return 1;
        cnt=1;
    }
    if(cnt>=k) return 1;
    return 0;
}
int main()
{
    int k;
    while(~scanf("%d%d",&n,&k))
    {
        m=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            m=max(m,a[i]);
        }
        m++;
        suffix();
        int l=0,r=n,ans=0;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(fun(mid,k))
            {
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值