• Milk Patterns poj3261(后缀数组)


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

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 #define N 20001
     6 #define M 1000002
     7 int a[N],c[N],d[N],e[N],sa[N],height[N],n,b[M],m,k;
     8 int cmp(int *r,int a,int b,int l)
     9 {
    10     return r[a]==r[b]&&r[a+l]==r[b+l];
    11 }
    12 void da()
    13 {
    14     int i,j,p,*x=c,*y=d,*t;
    15     memset(b,0,sizeof(b));
    16     for(i=0; i<n; i++)b[x[i]=a[i]]++;
    17     for(i=1; i<m; i++)b[i]+=b[i-1];
    18     for(i=n-1; i>=0; i--)sa[--b[x[i]]]=i;
    19     for(j=1,p=1; p<n; j*=2,m=p)
    20     {
    21         for(p=0,i=n-j; i<n; i++)y[p++]=i;
    22         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
    23         for(i=0; i<n; i++)e[i]=x[y[i]];
    24         for(i=0; i<m; i++)b[i]=0;
    25         for(i=0; i<n; i++)b[e[i]]++;
    26         for(i=1; i<m; i++)b[i]+=b[i-1];
    27         for(i=n-1; i>=0; i--)sa[--b[e[i]]]=y[i];
    28         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
    29             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    30     }
    31 }
    32 void callheight()
    33 {
    34     int i,j,k=0;
    35     b[0]=0;
    36     for(i=1; i<n; i++)b[sa[i]]=i;
    37     for(i=0; i<n-1; height[b[i++]]=k)
    38         for(k?k--:0,j=sa[b[i]-1]; a[i+k]==a[j+k]; k++);
    39 }
    40 bool check(int mid)
    41 {
    42     int sum,i=1;
    43     while(1)
    44     {
    45         while(i<n&&height[i]<mid)i++;
    46         if(i>=n)return 0;
    47         sum=1;
    48         while(i<n&&height[i]>=mid)
    49         {
    50             sum++;
    51             i++;
    52         }
    53         if(sum>=k)return 1;
    54     }
    55 }
    56 int main()
    57 {
    58     int i,l,r;
    59     scanf("%d%d",&n,&k);
    60     for(i=0; i<n; i++)scanf("%d",&a[i]),a[i]++,m=m<a[i]?a[i]:m;
    61     m=m<n?n:m;
    62     m++;
    63     a[n++]=0;
    64     da();
    65     callheight();
    66     l=0,r=n;
    67     while(l<=r)
    68     {
    69         int mid=(l+r)>>1;
    70         if(check(mid))
    71             l=mid+1;
    72         else r=mid-1;
    73     }
    74     printf("%d
    ",r);
    75 }
    View Code
  • 相关阅读:
    【CSS】盒子模型的计算
    【CSS】定义元素的位置
    【Jenkins】安装插件
    安装【Jenkins】
    Python【unittest】模块
    Python【pyyaml】模块
    CentOS下安装gcc和gdb
    汇编学习笔记(15)综合研究
    汇编学习笔记(14)BIOS对键盘输入的处理
    汇编学习笔记(13)直接定址表
  • 原文地址:https://www.cnblogs.com/ERKE/p/3593785.html
Copyright © 2020-2023  润新知