zoukankan      html  css  js  c++  java
  • POJ 3261

    Milk Patterns
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 11073   Accepted: 4949
    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
    找一个子串,至少出现了k次,而且长度最长
    先二分枚举子串的长度,然后再hash,看是否出现了k次
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<map>
     6 using namespace std;
     7 typedef unsigned long long ull;
     8 #define mmax 20000+10
     9 ull ansx[mmax],ansy[mmax],base[mmax],seed=131;
    10 int a[mmax];
    11 int n,k;
    12 bool judge(int len){
    13     memset(ansx,0,sizeof(ansx));
    14     for(int i=0;i<len;i++) ansx[0]=ansx[0]*seed+a[i] ;
    15     for(int i=len;i<n;i++) {
    16         ansx[i-len+1]=ansx[i-len]*seed-(a[i-len] )*base[len]+a[i] ;
    17     }
    18     sort(ansx,ansx+n-len+1);
    19     int cnt=1;
    20     for(int i=len;i<n;i++) {
    21         if(ansx[i-len+1]==ansx[i-len]) cnt++;
    22         else cnt=1;
    23         if(cnt>=k) return 1;
    24     }
    25     return 0;
    26 }
    27 int solve(){
    28     int left=0,right=n,mid,ans=0;
    29     while(left<=right){
    30         mid=(left+right)>>1;
    31         if(judge(mid)) ans=mid,left=mid+1;
    32         else right=mid-1;
    33     }
    34     return ans;
    35 }
    36 int main(){
    37     base[0]=1;
    38     for(int i=1;i<=20000;i++) base[i]=base[i-1]*seed;
    39     while(scanf("%d%d",&n,&k)!=EOF){
    40         for(int i=0;i<n;i++) scanf("%d",&a[i]);
    41         printf("%d
    ",solve());
    42     }
    43 }
  • 相关阅读:
    vim的分屏功能
    vim进阶
    VIM常用快捷键
    vim操作:打开多个文件、同时显示多个文件、在文件之间切换
    vim 如何复制文件中多行到另一个文件
    无限分类左右值算法的常规操作逻辑
    js查看Object对象的内容
    js获取当前页面的url信息
    javascript获取url中的参数值
    解决Eclipe安装不上android的ADT的办法
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/4227133.html
Copyright © 2011-2022 走看看