zoukankan      html  css  js  c++  java
  • Milk Patterns POJ

    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 ≤ KN) 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
      1 /*
      2 题意:
      3 给你一个长度为n的序列,你要找到重复相同至少K(2≤K≤N)次的样本的最长模式。输出最长是多少(这k次样本可以重叠一部分)
      4 
      5 题解:
      6 这道题原来我是想这先用后缀数组求出来height数组之后,然后只需要在height[i...n]这个范围内找到范围为m,使这个范围内的
      7 最小值尽可能大就行
      8 相当于滑动窗口,但是这个窗口的宽度是可变的(根据题目输入)
      9 然后我就发现了这个问题总是在询问一个范围内的最小值,感觉可用线段树。虽然每次询问耗时使log(n),但是要询问好多次,总复杂度
     10 相当于nlog(n),感觉还不行
     11 
     12 百度一发,可以用二分枚举最后答案,然后判断一下这个答案可以不就可以了
     13 怎么判断?
     14 你只需要找出来height数组中连着的m-1个height值都大于你二分枚举出来那个值就行
     15 
     16 */
     17 #include <cstdlib>
     18 #include <cstring>
     19 #include <cstdio>
     20 #include <algorithm>
     21 using namespace std;
     22 
     23 const int N = 200000+9;
     24 int x[N], y[N], c[N];
     25 int rank[N], height[N];
     26 int sa[N],s[N],n,k;
     27 bool pan(int *x,int i,int j,int k,int n)
     28 {
     29     int ti=i+k<n?x[i+k]:-1;
     30     int tj=j+k<n?x[j+k]:-1;
     31     return x[i]==x[j]&&ti==tj;
     32 }
     33 void build_SA(int n,int r)
     34 {
     35     int *x=rank,*y=height;
     36     for(int i=0; i<r; i++)c[i]=0;
     37     for(int i=0; i<n; i++)c[s[i]]++;
     38     for(int i=1; i<r; i++)c[i]+=c[i-1];
     39     for(int i=n-1; i>=0; i--)sa[--c[s[i]]]=i;
     40     r=1;
     41     x[sa[0]]=0;
     42     for(int i=1; i<n; i++)
     43         x[sa[i]]=s[sa[i]]==s[sa[i-1]]?r-1:r++;
     44     for(int k=1; r<n; k<<=1)
     45     {
     46         int yn=0;
     47         for(int i=n-k; i<n; i++)y[yn++]=i;
     48         for(int i=0; i<n; i++)
     49             if(sa[i]>=k)y[yn++]=sa[i]-k;
     50         for(int i=0; i<r; i++)c[i]=0;
     51         for(int i=0; i<n; i++)++c[x[y[i]]];
     52         for(int i=1; i<r; i++)c[i]+=c[i-1];
     53         for(int i=n-1; i>=0; i--)sa[--c[x[y[i]]]]=y[i];
     54         swap(x,y);
     55         r=1;
     56         x[sa[0]]=0;
     57         for(int i=1; i<n; i++)
     58             x[sa[i]]=pan(y,sa[i],sa[i-1],k,n)?r-1:r++;
     59     }
     60     for(int i=0; i<n; i++)rank[i]=x[i];
     61 }
     62 void get_height(int n)
     63 {
     64     int i,j,k=0;
     65     for(i=1; i<=n; i++)rank[sa[i]]=i;
     66     for(i=0; i<n; i++)
     67     {
     68         if(k)k--;
     69         else k=0;
     70         j=sa[rank[i]-1];
     71         while(s[i+k]==s[j+k])k++;
     72         height[rank[i]]=k;
     73     }
     74 }
     75 int check(int len)
     76 {
     77     int i=2,cnt=0;
     78     while(1)
     79     {
     80         while(i<=n && height[i]>=len)
     81             cnt++,i++;
     82         if(cnt+1>=k)return 1;
     83         if(i>=n)return 0;
     84         while(i <=n &&height[i]<len)
     85             i++;
     86         cnt=0;
     87     }
     88 }
     89 
     90 int main()
     91 {
     92     scanf("%d%d",&n,&k);
     93     for(int i=0; i<n; i++)
     94         scanf("%d",&s[i]),s[i]++;
     95     s[n]=0;
     96 
     97     build_SA(n+1,N);
     98     get_height(n);
     99     int l=1,r=n,ans=1,mid;
    100     while(l<=r)
    101     {
    102         mid=(l+r)/2;
    103         if(check(mid))
    104             l=mid+1,ans=mid;
    105         else
    106             r=mid-1;
    107     }
    108     printf("%d
    ",ans);
    109     return 0;
    110 }
  • 相关阅读:
    第四周作业
    第三周作业
    2019学期第八周编程总结
    2019学期第七周编程总结
    2019学期第六周编程总结 .
    2019学期第五周编程总结 .
    2019学期第四周编程总结 .
    2019学期第四周编程总结
    2019学期第三周编程总结
    2019年春季学期第二周作业
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/12322262.html
Copyright © 2011-2022 走看看