zoukankan      html  css  js  c++  java
  • poj 3261

    Milk Patterns
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 13249   Accepted: 5894
    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


    二分答案
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define MN 200003
    using namespace std;
    
    int n,m;
    int s[MN],a[MN];
    int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0;
    bool w[101];
    inline void gr(int x){
        rank[sa[1]]=1;
        for (int i=2;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1;
        for (int i=1;i<=n;i++) s[i]=rank[i];
    }
    inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e5;i++)v[i]+=v[i-1];}
    inline void gsa(){
        gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0);
        for (int i=1;i<n;i<<=1){
            gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
            for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j;
            for (int j=1;j<=n;j++) sa[j]=q[j];gr(i);
            if (rank[sa[n]]==n) return;
        }
    }
    inline void gh(){for (int i=1,k=0,j;i<=n;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
    int main(){
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++) scanf("%d",&a[i]),s[i]=a[i];
        gsa();gh();
        int l=0,r=n,mid,i,j,k,mmh;
        while(l<r){
            mid=(l+r+1)>>1;
            for (i=1,j,k=2;i<=n;i=k++){
                memset(w,0,sizeof(w));
                while (h[k]>=mid&&k<=n) k++;
                if (k-i>=m) break;
            }
            if (i<=n) l=mid;else r=mid-1;
        }
        printf("%d
    ",l);
    }
    1668K 94MS G++ 1288B
    
    
    
     
  • 相关阅读:
    SHELL编程之执行环境----邹立巍的博客
    linux内核分析系列--百度
    Linux模式设计系列( 内核与应用关联思考)
    Linux内核源代码情景分析系列
    Linux内核学习和研究及嵌入式(ARM)学习和研究的开放文档
    实验楼内核分析 +图
    《Linux内核修炼之道》 系列
    和菜鸟一起学linux内核源码之基础准备篇 系列 体系结构图
    实验楼在线算法学习
    linux-0.11内核 调试教程+GCC源代码
  • 原文地址:https://www.cnblogs.com/Enceladus/p/5462790.html
Copyright © 2011-2022 走看看