• poj3261 -- Milk Patterns


                                                                        Milk Patterns
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 13072   Accepted: 5812
    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 Ktimes.

    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次的最长子串长度,同poj1743,只是二分的时候改成如果同一组内元素个数大于等于K return 1
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<iostream>
     5 #include<algorithm>
     6 #define maxn 2000005
     7 int num[maxn],ws[maxn],wa[maxn],wb[maxn],sa[maxn];
     8 int rank[maxn],h[maxn],n,wv[maxn],K;
     9 bool cmp(int *r,int a,int b,int l){
    10     return r[a]==r[b]&&r[a+l]==r[b+l];
    11 }
    12 void da(int *r,int *sa,int n,int m){
    13     int *t,*x=wa,*y=wb,i,j,p;
    14     for (i=0;i<m;i++) ws[i]=0;
    15     for (i=0;i<n;i++) x[i]=r[i];
    16     for (i=0;i<n;i++) ws[x[i]]++;
    17     for (i=1;i<m;i++) ws[i]+=ws[i-1];
    18     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
    19     for (j=1,p=1;p<n;j*=2,m=p){
    20         for (p=0,i=n-j;i<n;i++) y[p++]=i;
    21         for (i=0;i<n;i++) if (sa[i]-j>=0) y[p++]=sa[i]-j;
    22         for (i=0;i<m;i++) ws[i]=0;
    23         for (i=0;i<n;i++) wv[i]=x[y[i]];
    24         for (i=0;i<n;i++) ws[wv[i]]++;
    25         for (i=1;i<m;i++) ws[i]+=ws[i-1];
    26         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
    27         for (t=x,x=y,y=t,i=1,p=1,x[sa[0]]=0;i<n;i++)
    28          x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
    29     } 
    30 }
    31 void cal(int *r,int n){
    32     int i,j,k=0;
    33     for (int i=1;i<=n;i++) rank[sa[i]]=i;
    34     for (int i=0;i<n;h[rank[i++]]=k)
    35      for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    36 }
    37 bool check(int x){
    38     int i=2,cnt;
    39     while (1){
    40         while (i<=n&&h[i]<x) i++;
    41         if (i>n) break;
    42         cnt=1;
    43         while (i<=n&&h[i]>=x){
    44             cnt++;
    45             i++;
    46         }
    47         if (cnt>=K) return 1;
    48     }
    49     return 0;
    50 }
    51 void work(){
    52     int l=1,r=n,ans;
    53     while (l<=r){
    54         int mid=(l+r)/2;
    55         if (check(mid)) l=mid+1,ans=mid;
    56         else r=mid-1;
    57     }
    58     printf("%d
    ",ans);
    59 }
    60 int main(){
    61     while (~scanf("%d%d",&n,&K)){
    62           for (int i=0;i<n;i++) scanf("%d",&num[i]);
    63           for (int i=0;i<n;i++) num[i]++;
    64           num[n]=0;
    65           da(num,sa,n+1,20005);
    66           cal(num,n);    
    67           work();
    68     }
    69 }
     
  • 相关阅读:
    每天一个设计模式(2):观察者模式
    每天一个设计模式(1):策略模式
    每天一个设计模式(0):设计模式概述
    常量池、栈、堆的比较
    常量池小结
    Java虚拟机体系结构分析
    Java并发(3):volatile及Java内存模型
    第一题:Big Countries
    22 高级SQL特性
    21 使用游标
  • 原文地址:https://www.cnblogs.com/qzqzgfy/p/5337171.html
Copyright © 2020-2023  润新知