poj3261 Milk Patterns

Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8475   Accepted: 3853
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
后缀数组,对于这题,我们可以知道,二分长度,定成判定性问题,然后,用height数组分成几段,如果,有连续的一段的height值大于k,那么,就是可行解!如果有连续的k-1个height[]大于或等于 length,就可知满足有k个重复的可重叠的字串出现!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 1000500
int cnt[200000],wa[maxn],wb[maxn],ws[maxn],wv[maxn],wsd[maxn],r[maxn],ans[maxn];
int str[maxn];
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int c0(int *r,int a,int b)
{return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}
int c12(int k,int *r,int a,int b)
{if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i=0;i<n;i++) wv[i]=r[a[i]];
    for(i=0;i<m;i++) wsd[i]=0;
    for(i=0;i<n;i++) wsd[wv[i]]++;
    for(i=1;i<m;i++) wsd[i]+=wsd[i-1];
    for(i=n-1;i>=0;i--) b[--wsd[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m)
{
    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    r[n]=r[n+1]=0;
    for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0;i<tbc;i++) san[rn[i]]=i;
    for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0;i<ta && j<tbc;p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(;i<ta;p++) sa[p]=wa[i++];
    for(;j<tbc;p++) sa[p]=wb[j++];
    return;
}
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0;i<m;i++) wsd[i]=0;
    for(i=0;i<n;i++) wsd[x[i]=r[i]]++;
    for(i=1;i<m;i++) wsd[i]+=wsd[i-1];
    for(i=n-1;i>=0;i--) sa[--wsd[x[i]]]=i;
    for(j=1,p=1;p<n;j*=2,m=p)
    {
        for(p=0,i=n-j;i<n;i++) y[p++]=i;
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(i=0;i<n;i++) wv[i]=x[y[i]];
        //м╟ее
        for(i=0;i<m;i++) wsd[i]=0;
        for(i=0;i<n;i++) wsd[wv[i]]++;
        for(i=1;i<m;i++) wsd[i]+=wsd[i-1];
        for(i=n-1;i>=0;i--) sa[--wsd[wv[i]]]=y[i];
        //м╟ее
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return;
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n){
    int i,j,k=0;
    for(i=1;i<=n;i++) rank[sa[i]]=i;
    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++);
    return;
}
int judge(int x,int n,int k){
    cnt[0]=0;
    for(int i=1;i<=n;i++){
        if(height[i]>=x)cnt[i]=cnt[i-1]+1;
        else cnt[i]=0;
        if(cnt[i]>=k-1)return 1;
    }
    return 0;
}
int solve(int n,int k){
   int l=0,r=n,mid,last=-1;
   while(l<=r){
        mid=(l+r)>>1;
        if(judge(mid,n,k))l=mid+1,last=mid;
        else r=mid-1;
   }
   return last;
}
int main()
{
    int n,n1,i,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        for( i=0;i<n;i++)
        scanf("%d",&r[i]),r[i]++;
        r[n]=0;
        int m=1000005;
        da(r,ans,n+1,m);
        //dc3(r,ans,n+1,m);
        calheight(r,ans,n);
        int maxx=solve(n,k);
        printf("%d\n",maxx);
    }
    return 0;
}


你可能感兴趣的:(poj3261 Milk Patterns)