2018.07.17 牛奶模式Milk Patterns(二分+hash)

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

  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;
}

转载于:https://www.cnblogs.com/ldxcaicai/p/9738444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值