【poj3261】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

Source

USACO 2006 December Gold

求可重复的连续出现不少于k次的最长子串

二分后看看lcp是否有k-1个大于k即可

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m;

const int SZ = 1000010;

int lcp[SZ],sa[SZ],rank[SZ],k = 1,tmp[SZ];

bool cmp_sa(int i,int j)
{
    if(rank[i] != rank[j]) return rank[i] < rank[j];
    else
    {
        int x = i + k <= n ? rank[i + k] : -1;
        int y = j + k <= n ? rank[j + k] : -1;
        return x < y;
    }
}

void get_sa(int s[])
{
    for(int i = 0;i <= n;i ++)
    {
        sa[i] = i;
        rank[i] = i == n ? -1 : s[i];
    }
    for(k = 1;k <= n;k <<= 1)
    {
        sort(sa,sa + 1 + n,cmp_sa);

        tmp[sa[0]] = 0;
        for(int i = 1;i <= n;i ++)
            tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0);
        for(int i = 0;i <= n;i ++)
            rank[i] = tmp[i];
    }
}

void get_lcp(int s[])
{
    for(int i = 0;i <= n;i ++)
        rank[sa[i]] = i;
    int h = 0;
    lcp[0] = 0;
    for(int i = 1;i <= n;i ++)
    {
        int j = sa[rank[i] - 1];
        if(h) h --;
        while(i + h < n && j + h < n)
        {
            if(s[i + h] == s[j + h]) h ++;
            else break;
        }
        lcp[rank[i] - 1] = h;
    }
}

bool check(int len)
{
    int tmp = 0;
    for(int i = 1;i < n;i ++)
    {
        if(lcp[i] >= len)
        {
            if(++ tmp >= m - 1)
                return true;
        }
        else
            tmp = 0;
    }
    return false;
}

int div()
{
    int l = -1,r = n;
    while(r - l > 1)
    {
        int mid = (l + r) >> 1;
        if(check(mid)) l = mid;
        else r = mid;
    }
    return l;
}

int s[SZ];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 0;i < n;i ++)
        scanf("%d",&s[i]);
    get_sa(s);
    get_lcp(s);
    printf("%d\n",div());
    return 0;
}

你可能感兴趣的:(【poj3261】Milk Patterns 后缀数组+二分)