【POJ3261】 Milk Patterns 后缀数组

Milk Patterns
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12990 Accepted: 5775
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次的最长串
Solution:二分答案,判定连续Height【i-j】满足,且(j-i)+1>=k-1

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int wa[20055],wb[20055],sa[20055],rank[20055],height[20055];
int n;
int r[20055];
int k;
int cnt[20055];
void DA(int n,int m)
{
    int *x=wa,*y=wb;
    for(int i=0;i<m;i++) cnt[i]=0;
    for(int i=0;i<n;i++) cnt[x[i]=r[i]]++;
    for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];
    for(int i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i;
    for(int p=1,j=1;p<n;m=p,j<<=1)
    {
        p=0;
        for(int i=n-j;i<n;i++) y[p++]=i;
        for(int i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(int i=0;i<m;i++) cnt[i]=0;
        for(int i=0;i<n;i++) cnt[x[y[i]]]++;
        for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i];
        swap(x,y);
        x[sa[0]]=0;
        p=1;
        for(int i=1;i<n;i++)
            x[sa[i]]=((y[sa[i]]==y[sa[i-1]])&&y[sa[i]+j]==y[sa[i-1]+j])?p-1:p++;
    }
}
void cal_H(int n)
{
    for(int i=1;i<=n;i++) rank[sa[i]]=i;
    int k=0,i,j;
    for(i=0;i<n;height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);    
}

bool C(int x)
{
        for(int i=2;i<=n;i++)
        if(height[i]>=x)
        {
            int cmt=1;
            for(i;i<=n&&height[i]>=x;i++) cmt++;
            if(cmt>=k) return 1;    
        }
        return 0;
}
int solve()
{
    int ans=0;
    int ls=0,rs=n;
    while(ls<=rs)
    {
        int mid=(ls+rs)>>1;
        if(C(mid))  ans=mid, ls=mid+1;
        else rs=mid-1;
    }
    return ans;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    scanf("%d",&r[i]);
    r[n]=0;
    DA(n+1,20005);
    cal_H(n);
    printf("%d\n",solve());             
}

你可能感兴趣的:(poj)