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;
        
    }
    
    
    
     
  • 相关阅读:
    Wannafly挑战赛13 C:zzf的好矩阵(思维)
    Wannafly挑战赛13 B:Jxc军训(逆元)
    TZOJ 1221 Tempter of the Bone(回溯+剪枝)
    AtCoder Regular Contest 092 C
    TZOJ 3030 Courses(二分图匹配)
    TOJ 2778 数据结构练习题――分油问题(广搜和哈希)
    PAT L3-001 凑零钱(01背包dp记录路径)
    [HNOI2009]通往城堡之路
    [HNOI2006]潘多拉的宝盒
    [bzoj4361]isn
  • 原文地址:https://www.cnblogs.com/BriMon/p/9652658.html
Copyright © 2011-2022 走看看