传送门

一道简单的字符串。这里收集了几种经典做法:

  1. SAM,不想写。
  2. 后缀数组+二分,不想写
  3. 后缀数组+单调队列,不想写
  4. hash+二分,for循哈希,天下无敌!于是妥妥的hash

代码如下:

#include<bits/stdc++.h>
#define N 20005
#define Base 20001
using namespace std;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans;
}
inline void write(int x){
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
int n,a[N],b[N],k;
map<unsigned int,int>mp;
unsigned int sum[N],bas[N];
int main(){
    n=read(),k=read();
    bas[0]=1;
    for(int i=1;i<=n;++i)a[i]=b[i]=read(),bas[i]=bas[i-1]*Base;
    sort(b+1,b+n+1);
    int siz=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;++i){
        a[i]=lower_bound(b+1,b+siz+1,a[i])-b;
        sum[i]=sum[i-1]*Base+a[i];
    }
    int l=0,r=n;
    while(l<=r){
        mp.clear();
        int mid=l+r>>1,cnt=0;
        for(int i=1;i+mid<=n;++i)cnt=max(cnt,++mp[sum[i+mid]-sum[i-1]*bas[mid+1]]);
        if(cnt>=k)l=mid+1;
        else r=mid-1;
    }
    printf("%d",l);
    return 0;
}

2018.07.17 牛奶模式Milk Patterns(二分+hash)的更多相关文章

  1. [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns

    洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...

  2. luogu P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组 + Height数组 + 二分答案 + 扫描

    后缀数组有一个十分有趣的性质: $height[rk[i]] >= height[rk[i-1]] - 1$    Code: #include <bits/stdc++.h> #d ...

  3. 【后缀数组】【LuoguP2852】 [USACO06DEC]牛奶模式Milk Patterns

    题目链接 题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". J ...

  4. [Luogu2852][USACO06DEC]牛奶模式Milk Patterns

    Luogu 一句话题意 给出一个串,求至少出现了\(K\)次的子串的最长长度. sol 对这个串求后缀数组. 二分最长长度. 如果有\(K\)个不同后缀他们两两的\(lcp\)都\(>=mid\ ...

  5. 洛谷P2852 牛奶模式Milk Patterns [USACO06DEC] 字符串

    正解:SA/二分+哈希 解题报告: 传送门! umm像这种子串的问题已经算是比较套路的了,,,?就后缀的公共前缀这样儿的嘛QwQ 所以可以先求个SA 然后现在考虑怎么判断一个长度为d的子串出现了k次? ...

  6. [USACO06DEC] 牛奶模式Milk Patterns

    题目链接:戳我 我们知道后缀数组的h数组记录的是后缀i和后缀i-1的最长公共前缀长度,后缀的前缀其实就是子串. 因为是可以重复出现的子串,所以我们只要计算哪些h数组的长度大于等于x即可.这一步操作我们 ...

  7. P2852 [USACO06DEC]牛奶模式Milk Patterns

    link 这是一道后缀匹配的模板题 我们只需要将height算出来 然后二分一下答案就可以了 #include<cstdio> #include<algorithm> #inc ...

  8. Luogu P2852 [USACO06DEC]牛奶模式Milk Patterns

    题目链接 \(Click\) \(Here\) 水题.利用\(Height\)的性质维护一个单调栈即可. #include <bits/stdc++.h> using namespace ...

  9. 2018.07.03 POJ 2318 TOYS(二分+简单计算几何)

    TOYS Time Limit: 2000MS Memory Limit: 65536K Description Calculate the number of toys that land in e ...

随机推荐

  1. Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/co

    转自:https://www.xuebuyuan.com/934357.html 需要引入standard.jar和jstl.jar 正确添加即可

  2. Delphi 浏览器WebBrowser

    WebBrowser1.Navigate(URL); while WebBrowser1.busy do Application.ProcessMessages; while WebBrowser1. ...

  3. getitem, setitem, delitem (把类实例化成字典的类型)

    class Foo(object):     def __init__(self):         self.data = {} def __getitem__(self, key):        ...

  4. hibernate 三种状态

    JPA是Java Persistence API的简称,中文名Java持久层API. 因JPA是由Hibernate的创建者编写,因此和Hibernate基本相似一致.JPA由不同的服务解析,因此在w ...

  5. 离线安装Cloudera Manager 5和CDH5

    关于CDH和Cloudera Manager CDH (Cloudera's Distribution, including Apache Hadoop),是Cloudera 完全开源的Hadoop  ...

  6. Containerpilot 配置文件模板

    { "consul": "{{ .CONSUL }}:8500", "logging": { "level": &quo ...

  7. Linux下php5.3.3安装mcrypt扩展

    具体操作: 一.下载软件包 1.下载php(版本要与系统安装的一致) http://pan.baidu.com/s/1mifTbfE 2.下载libmcrypt(安装mcrypt需要此软件包) htt ...

  8. php 过滤emoji

    function filter_emoji_string($str){ $str = trim($str); $str = preg_replace_callback('/./u',function ...

  9. Ubuntu下Anaconda3的安装

    1)去https://www.anaconda.com/download/#download下载Anaconda安装文件(python3.6的版本). 2)进入到Anaconda3-5.0.1-Lin ...

  10. vs2015安装出问题

    win7系统需要更新serverpage1包,更新完就ok了,ie不用升级到ie10