zoukankan      html  css  js  c++  java
  • Milk Patterns (hash + 二分)

    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次。

    思路:二分枚举长度L,然后记录从每个位置开始长度为L的字符串的哈希值,最后去遍历这些哈希值看看想等的数量是否大于等于k

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <stdlib.h>
     5 #include <string>
     6 #include <string.h>
     7 #include <math.h>
     8 #include <vector>
     9 #include <queue>
    10 #include <stack>
    11 #include <map>
    12 #include <set>
    13 
    14 
    15 #define INF 0x3f3f3f3f
    16 #define LL long long
    17 
    18 typedef unsigned long long ull;
    19 const int maxn = 1e5+10;
    20 
    21 int a[maxn];
    22 int s[maxn];
    23 ull base = 131;
    24 ull mod = 1e9+7;
    25 ull p[maxn];
    26 ull h1[maxn],h2[maxn];
    27 ull q[maxn];
    28 int n,k;
    29 
    30 ull get_hash(ull h[],int l,int r){
    31     return (h[r] - h[l-1]*p[r-l+1]);
    32 }
    33 
    34 bool check(int len) {
    35     int cnt = 0;
    36     for (int i=1;i+len-1<=n;i++) {
    37         q[cnt++] = get_hash(h1,i,i+len-1);
    38     }
    39     std::sort(q,q+cnt);
    40     for (int i=0;i<cnt;i++) {
    41         int ans = 0;
    42         int j;
    43         for (j=i;j<cnt;j++) {
    44             if (q[i] == q[j])
    45                 ans++;
    46             else
    47                 break;
    48         }
    49         i = j-1;  //可以稍微剪一下
    50         if (ans>=k)
    51             return true;
    52     }
    53     return false;
    54 }
    55 
    56 
    57 
    58 
    59 int main(){
    60     std::cin >> n >> k;
    61     p[0] = 1;
    62     for (int i=1;i<maxn;i++) {
    63         p[i] = p[i-1] * base;
    64     }
    65     for (int i=1;i<=n;i++) {
    66         std::cin >> s[i];
    67     }
    68     for (int i=1;i<=n;i++) {
    69         h1[i] = h1[i-1] * base + s[i];
    70     }
    71     int l = 0,r = n;
    72     int ans = 0;
    73     while (l <= r) {
    74         int mid = (l + r) >> 1;
    75         if (check(mid)) {
    76             ans = mid;
    77             l = mid + 1;
    78         }
    79         else
    80             r = mid - 1;
    81     }
    82     printf("%d
    ",ans);
    83     return 0;
    84 }
  • 相关阅读:
    驱动下的异常处理
    头文件 .h 与源文件 .ccp 的区别
    驱动程序进阶篇
    驱动中链表的使用
    内存操作相关内核 API 的使用
    链表的概念、建立、删除与插入
    编写简单的 NT 式驱动程序的加载与卸载工具
    驱动程序入门篇
    c++ 指针的简单用法
    CTL_CODE 宏 详解
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11955005.html
Copyright © 2011-2022 走看看