码迷,mamicode.com
首页 > 编程语言 > 详细

POJ3261(后缀数组+2分枚举)

时间:2016-03-01 22:13:36      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:

Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12972   Accepted: 5769
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
思路:用后缀数组求出lcp后,2分枚举L使得连续的lcp[i]>=L 的个数>=k-1;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1000005;
int buf[MAXN];
int sa[MAXN];
int rnk[MAXN];
int tmp[MAXN];
int lcp[MAXN];
int len,k;
int t;

bool comp(int i,int j)
{
    if(rnk[i]!=rnk[j])    return rnk[i]<rnk[j];
    else
    {
        int ri=(i+k<=len)?rnk[i+k]:-1;
        int rj=(j+k<=len)?rnk[j+k]:-1;
        return ri<rj;
    }
}

void getsa()
{
    memset(sa,0,sizeof(sa));
    memset(rnk,0,sizeof(rnk));
    memset(tmp,0,sizeof(tmp));
    
    for(int i=0;i<len;i++)
    {
        sa[i]=i;
        rnk[i]=buf[i];
    }
    sa[len]=len;
    rnk[len]=-1;
    
    for(k=1;k<=len;k*=2)
    {
        sort(sa,sa+len+1,comp);
        
        tmp[sa[0]]=0;
        for(int i=1;i<=len;i++)
        {
            tmp[sa[i]]=tmp[sa[i-1]]+(comp(sa[i-1],sa[i])?1:0);
        }
        
        for(int i=0;i<=len;i++)
        {
            rnk[i]=tmp[i];    
        }
    }
    
}

void getlcp()
{
    getsa();
    memset(rnk,0,sizeof(rnk));
    memset(lcp,0,sizeof(lcp));
    for(int i=0;i<=len;i++)
    {
        rnk[sa[i]]=i;
    }
    
    int h=0;
    lcp[0]=h;
    for(int i=0;i<len;i++)
    {
        int j=sa[rnk[i]-1];
        if(h>0)    h--;
        for(;i+h<len&&j+h<len;h++)
        {
            if(buf[i+h]!=buf[j+h])    break;
        }
        lcp[rnk[i]-1]=h;
    }
    
}

void debug()
{
    for(int i=0;i<=len;i++)
    {
        int l=sa[i];
        if(l==len)
        {
            printf("0\n");
        }
        else
        {
            for(int j=sa[i];j<len;j++)
            {
                printf("%d ",buf[j]);
            }    
            printf("     %d\n",lcp[i]);
        }
    }
    
}

bool judge(int l)
{
    int  cnt=0;
    for(int i=1;i<len;i++)
    {
        if(lcp[i]>=l)//求前缀大于等于l的连续长度 
        {
            cnt++;
        }
        else
            cnt=0;
        if(cnt==t-1)    return true;
    }
    return false;
}

void solve()
{
    
    int l=1,r=len;
    int ans=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(judge(mid))//2分枚举长度 
        {
            ans=max(ans,mid);
            l=mid+1;
        }
        else    r=mid-1;
    }
    printf("%d\n",ans);
}

int main()
{
    while(scanf("%d%d",&len,&t)!=EOF)
    {
        for(int i=0;i<len;i++)
            scanf("%d",&buf[i]);
        getlcp();
    //    debug()
        solve();
    }
    return 0;
}

 



POJ3261(后缀数组+2分枚举)

标签:

原文地址:http://www.cnblogs.com/program-ccc/p/5232495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!