zoukankan      html  css  js  c++  java
  • [Luogu2852][USACO06DEC]牛奶模式Milk Patterns

    题目描述

    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.

    农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

    输入输出格式

    输入格式:

    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.

    输出格式:

    Line 1: One integer, the length of the longest pattern which occurs at least K times

    输入输出样例

    输入样例#1: 复制
    8 2
    1
    2
    3
    2
    3
    2
    3
    1
    输出样例#1: 复制
    4




    题解都是后缀数组。。好厉害啊。。。
    所以可以用二分加哈希做23333.
    二分长度,然后每个长为mid的哈希值都拿出来比较一下,如果相同的大于k就返回1,否则返回0.
    水过去了。


     
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    #define reg register 
    inline int read() {
        int res = 0;char ch=getchar();bool fu=0;
        while(!isdigit(ch)) {if(ch=='-')fu=1;ch=getchar();}
        while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48), ch=getchar();
        return fu?-res:res;
    }
    #define ull unsigned long long
    int n, k;
    ull Hash[20005], p[20005];
    int ans;
    ull tmp[20005];
    
    inline bool check(int mid)
    {
        for (reg int i = mid ; i <= n ; i ++)
            tmp[i] = Hash[i] - Hash[i-mid] * p[mid];
        sort(tmp + mid, tmp + 1 + n);
        int res = 1;
        for (reg int i = mid + 1 ; i <= n ; i ++)
        {
            if (tmp[i] == tmp[i-1]) 
            {
                res++;
                if (res >= k) return 1;
            }
            else res = 1;
        }
        return 0;
    }
    
    int main()
    {
        n = read(), k = read();
        for (reg int i = 1 ; i <= n ; i ++) 
        {
            int x = read();
            Hash[i] = Hash[i-1] * 11 + x;
        }
        p[0] = 1;
        for (reg int i = 1 ; i <= n ; ++ i) p[i] = p[i-1] * 11;
        int l = 1, r = n;
        while(l <= r)
        {
            int mid = l + r >> 1;
            if (check(mid)) l = mid + 1, ans = mid;
            else r = mid - 1;
        }
        cout << ans << endl;
        return 0;
        
    }
    
    
    
     
  • 相关阅读:
    TVM性能评估分析(七)
    TVM性能评估分析(六)
    理论三:里式替换(LSP)跟多态有何区别?哪些代码违背了LSP?
    理论二:如何做到“对扩展开放、修改关闭”?扩展和修改各指什么?
    (转)浅谈 SOLID 原则的具体使用
    老一辈的革命先烈告诉我,凡事打好提前量
    理论一:对于单一职责原则,如何判定某个类的职责是否够“单一”?
    实战二(下):如何利用面向对象设计和编程开发接口鉴权功能?
    实战二(上):如何对接口鉴权这样一个功能开发做面向对象分析?
    12 | 实战一(下):如何利用基于充血模型的DDD开发一个虚拟钱包系统?
  • 原文地址:https://www.cnblogs.com/BriMon/p/9652658.html
Copyright © 2011-2022 走看看