zoukankan      html  css  js  c++  java
  • poj 3261 后缀数组 可重叠的 k 次最长重复子串

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

    Source

    题意:
    求可重叠的 k 次最长重复子串
    代码:
    //论文题,同样二分判断,heigh数组分组统计个数
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int INF=0x7fffffff;
    const int MAXN=1000000;
    int he[MAXN+9],ra[MAXN+9],sa[MAXN+9],xx[MAXN+9],yy[MAXN+9],buc[MAXN+9];
    int s[MAXN+9],len,m;
    void get_suf()
    {
        int *x=xx,*y=yy;
        for(int i=0;i<m;i++) buc[i]=0;
        for(int i=0;i<len;i++) buc[x[i]=s[i]]++;
        for(int i=1;i<m;i++) buc[i]+=buc[i-1];
        for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i;
        for(int k=1;k<=len;k<<=1){
            int p=0;
            for(int i=len-1;i>=len-k;i--) y[p++]=i;
            for(int i=0;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
            for(int i=0;i<m;i++) buc[i]=0;
            for(int i=0;i<len;i++) buc[x[y[i]]]++;
            for(int i=1;i<m;i++) buc[i]+=buc[i-1];
            for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(int i=1;i<len;i++){
                if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
                    x[sa[i]]=p-1;
                else x[sa[i]]=p++;
            }
            if(p>=len) break;
            m=p;
        }
        for(int i=0;i<len;i++) ra[sa[i]]=i;
        int k=0;
        for(int i=0;i<len;i++){
            if(ra[i]==0) { he[0]=0; continue; }
            if(k) k--;
            int j=sa[ra[i]-1];
            while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
            he[ra[i]]=k;
        }
    }
    bool solve(int mid,int k)
    {
        int cnt=0;
        for(int i=1;i<len;i++){
            if(he[i]>=mid){
                cnt++;
                if(cnt+1>=k) return 1;
            }else cnt=0;
        }
        return 0;
    }
    int main()
    {
        int n,k;
        m=-INF;
        while(scanf("%d%d",&n,&k)==2){
            for(int i=0;i<n;i++){
                scanf("%d",&s[i]);
                m=max(m,s[i]);
            }
            len=n;m+=2;
            get_suf();
            int l=0,r=len,ans=0;
            while(l<=r){
                int mid=(l+r)>>1;
                if(solve(mid,k)) { ans=mid;l=mid+1; }
                else r=mid-1;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    是否是轮回(续)
    夜雨做成秋
    53分
    浮生六记 一成长星和月
    企业信息化常见缩略语汇总
    是否是轮回
    对信号集操作函数的使用方法和顺序
    fcntl.h
    关于linux信号量的基本使用
    linux 共享内存
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7598644.html
Copyright © 2011-2022 走看看