poj 3261 Milk Patterns(后缀数组-最长重复子串)

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

题意:给你一个n,k,一个长度为n的串,问你最长至少重复k次的子串的长度是多少。这题里的重复的子串是可以叠加的,就是重复的这些子串是可以有重合部分的。

我们可以二分这个答案,然后判定这个答案合不合法。那么问题就是怎么判定答案合不合法的问题了。

我们知道高度数组的值是相邻排名的两个后缀的最长公共前缀的长度,两个不相邻的后缀的最长公共前缀的长度就是这两个排名之间高度数组的最小值,那么要计算一个子串重复了多少次,那么只用计算一个连续的都大于我们二分出的值的区间的长度。

对于这个样例,按排名把后缀排序:

12323231
231      
23231      
2323231   
31 
3231 
323231 

对于二分出来的x==3,我们发现最后两个后缀是满足重复次数大于等于k的,对于二分出来的x==4,第三到第四个后缀是满足重复次数大于等于k的。

#include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>

using namespace std;

const int MAXN = 200005;


int SA[MAXN], Rank[MAXN], Height[MAXN], tax[MAXN], tp[MAXN], a[MAXN], n, m;
char str[MAXN];

void RSort()
{
    for (int i = 0; i <= m; i ++) tax[i] = 0;
    for (int i = 1; i <= n; i ++) tax[Rank[tp[i]]] ++;
    for (int i = 1; i <= m; i ++) tax[i] += tax[i-1];
    for (int i = n; i >= 1; i --) SA[tax[Rank[tp[i]]] --] = tp[i];
}
int cmp(int *f, int x, int y, int w) { return (f[x] == f[y] && f[x + w] == f[y + w]); }


void Suffix()
{
    for (int i = 1; i <= n; i ++) Rank[i] = a[i], tp[i] = i;
    m = 256 ,RSort();

    for (int w = 1, p = 1, 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(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);
}


void Init()
{
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    Suffix();
}
int k;

bool judge(int x)
{
    int i = 1;
    while(i <= n)
    {
        int j = i+1;
        int cnt = 1;
        while(Height[j] >= x && j <= n)
        {
            cnt++;
            j++;
        }
        if(cnt >= k)
            return true;
        i = j;
    }

    return false;
}
int binarysearch()
{
    int l = 1,r = n;
    int ans = l;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(judge(mid))
        {
            l = mid + 1;
            ans = mid;
        }
        else
            r = mid - 1;
    }

    return ans;
}
int main(void)
{
    while(scanf("%d%d",&n,&k)==2)
    {
        Init();
        int ans = binarysearch();
        printf("%d\n",ans);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值