zoukankan      html  css  js  c++  java
  • [USACO06DEC] Milk Patterns

    题目描述

    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.

    农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

    输入输出格式

    输入格式:

    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.

    输出格式:

    Line 1: One integer, the length of the longest pattern which occurs at least K times

    输入输出样例

    输入样例#1: 
    8 2
    1
    2
    3
    2
    3
    2
    3
    1

    输出样例#1: 
    4

    (好像二分+HASH也可以做的样子)
    求出后缀数组和height之后,我们按height从大到小再排一次序,然后用并查集从前到后合并,如果某次合并后siz>=k了,那么直接输出当前的height即可。
    #include<bits/stdc++.h>
    #define ll long long
    #define maxn 40005
    using namespace std;
    int cc[maxn],n,m,k,r[maxn];
    int sa[maxn],sax[maxn],sz[maxn];
    int rank[maxn],rankx[maxn];
    int sec[maxn],ans=0,p[maxn];
    int s[maxn],a[maxn],ky,height[maxn];
    
    inline bool cmp(int x,int y){
        return height[x]>height[y];
    }
    
    int ff(int x){
        return (p[x]==x?x:(p[x]=ff(p[x])));
    }
    
    inline void prework(){
        for(int i=0;i<n;i++) cc[s[i]]++;
        for(int i=1;i<=n;i++) cc[i]+=cc[i-1];
        for(int i=0;i<n;i++) sa[cc[s[i]]--]=i;
        for(int i=1;i<=n;i++){
            rank[sa[i]]=i;
            if(i>1&&s[sa[i]]==s[sa[i-1]]) rank[sa[i]]=rank[sa[i-1]];
        }
        
        int l=1;
        while(l<n){
            memset(cc,0,sizeof(cc));
            for(int i=0;i<n;i++) cc[sec[i]=rank[i+l]]++;
            for(int i=n-1;i>=0;i--) cc[i]+=cc[i+1];
            for(int i=0;i<n;i++) sax[cc[sec[i]]--]=i;
            
            memset(cc,0,sizeof(cc));
            for(int i=0;i<n;i++) cc[rank[i]]++;
            for(int i=1;i<=n;i++) cc[i]+=cc[i-1];
            for(int i=1;i<=n;i++) sa[cc[rank[sax[i]]]--]=sax[i];
            
            for(int i=1;i<=n;i++){
                rankx[sa[i]]=i;
                if(i>1&&rank[sa[i]]==rank[sa[i-1]]&&sec[sa[i]]==sec[sa[i-1]]) rankx[sa[i]]=rankx[sa[i-1]];
            }
            
            for(int i=0;i<n;i++) rank[i]=rankx[i];
            l<<=1;
        }
        
        int now=0,j,mx;
        for(int i=0;i<n;i++){
            if(rank[i]==1){
                now=0,height[1]=0;
                continue;
            }
            
            if(now) now--;
            int j=sa[rank[i]-1],mx=max(i,j);
            while(mx+now<n&&s[i+now]==s[j+now]) now++;
            
            height[rank[i]]=now;
        }
        
        for(int i=1;i<=n;i++) r[i]=i,p[i]=i,sz[i]=1;
        sort(r+1,r+n+1,cmp);
    
        int u,v,fa,fb;
        for(int i=1;i<=n;i++){
            u=r[i],v=r[i]-1;
            if(!v) continue;
            
            fa=ff(u),fb=ff(v);
            if(fa!=fb){
                p[fa]=fb;
                sz[fb]+=sz[fa];
            }
            
            if(sz[fb]>=k){
                printf("%d
    ",height[r[i]]);
                return;
            }
        }
    }
    
    int main(){
        scanf("%d%d",&n,&k);
        for(int i=0;i<n;i++) scanf("%d",s+i),a[i+1]=s[i];
        sort(a+1,a+n+1);
        ky=unique(a+1,a+n+1)-a-1;
        for(int i=0;i<n;i++) s[i]=lower_bound(a+1,a+n+1,s[i])-a;
        prework();
        
        return 0;
    }
    
    
  • 相关阅读:
    分布式文件系统
    分布式系统中的CAP理论
    安装Elasticsearch-header插件
    Elasticsearch 安装
    分布式搜索引擎-ES
    高可用集群架构 Keepalived 双机主备和双主热备
    先阶段部署架构
    技术人员的两个发展方向
    input标签写CSS时需要注意的几点(先收藏)
    如何设置box shadow的透明度
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8321135.html
Copyright © 2011-2022 走看看