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


    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
    ");
            }
            else
            {
                for(int j=sa[i];j<len;j++)
                {
                    printf("%d ",buf[j]);
                }    
                printf("     %d
    ",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
    ",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;
    }


  • 相关阅读:
    #动态规划 0-1背包问题空间复杂度优化
    #动态规划 0-1背包问题思路概述
    #动态规划 LeetCode 337 打家劫舍 III
    #动态规划 LeetCode 213 打家劫舍 II
    #动态规划 LeetCode 198 打家劫舍
    #动态规划 LeetCode 63 不同路径 II
    #动态规划 LeetCode 62 不同路径
    #动态规划 LeetCode 279 完全平方数
    #动态规划 LeetCode 343 整数拆分
    #动态规划 LeetCode 64 最小路径和
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5232495.html
Copyright © 2020-2023  润新知