传送门

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

  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. js 层随着滚动条上下移动

    var tips; var theTop = 10; /*这是默认高度,越大越往下*/ var old = theTop; function moveTips() { var tt = 0; if ( ...

  2. Java Magic. Part 2: 0xCAFEBABE

    Java Magic. Part 2: 0xCAFEBABE @(Base)[JDK, magic, 黑魔法] 转载请写明:原文地址 英文原文 系列文章: -Java Magic. Part 1: j ...

  3. canvas 移动光速特效-

    http://pan.baidu.com/s/1cHtABO 密码:istl

  4. KEGG数据库介绍

    转载自https://mp.weixin.qq.com/s/pqbMXMkuqEXbLf31PTxGZQ KEGG简介 KEGG 数据库于 1995 年由 Kanehisa Laboratories ...

  5. update from

    update table1 set table1.column1 =(select table2.column1 from table2  where 关联条件) where exists(selec ...

  6. java基础六 [异常处理](阅读Head First Java记录)

    在程序运行时,我们不能保证所有服务和方法都是正确的,如果发生问题报错会导致程序崩溃,所以需要对一些可以预见的错误进行异常处理,通过throw去抛出一个异常,然后用try..catch..将要执行的该方 ...

  7. sqlserver批量导出存储过程、函数、视图

    select text from syscomments s1 join sysobjects s2 on s1.id=s2.id  where xtype = 'V' xtype V   视图 P  ...

  8. 第五章 二叉树(c)二叉树

  9. 第五章 二叉树(a)树

  10. Chip Factory(01字典树)

    Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others)  ...