bzoj1717[Usaco2006 Dec]Milk Patterns 产奶的模式*「建议收藏」

bzoj1717[Usaco2006 Dec]Milk Patterns 产奶的模式*

大家好,又见面了,我是全栈君。

bzoj1717[Usaco2006 Dec]Milk Patterns 产奶的模式

题意:

John记录了n天的牛奶质量值。他想知道最长的出现了至少k次的模式(即一个连续子串)的长度。n≤20000。

题解:

求一个总哈希值,二分模式长度,枚举每个模式开始端点,获得它的哈希值,然后排序比较有没有至少k个相等。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 20010
 6 #define ll unsigned long long
 7 #define yyl 2333
 8 using namespace std;
 9 
10 inline int read(){
11     char ch=getchar(); int f=1,x=0;
12     while(ch<'0'||ch>'9'){
   
   if(ch=='-')f=-1; ch=getchar();}
13     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
14     return f*x;
15 }
16 ll hash[maxn],mi[maxn],calc[maxn],pat[maxn]; int n,k,l,r,ans,tot;
17 int main(){
18     n=read(); k=read(); inc(i,1,n)pat[i]=read(); mi[0]=1;
19     inc(i,1,n)hash[i]=hash[i-1]*yyl+pat[i],mi[i]=mi[i-1]*yyl; l=1; r=n;
20     while(l<=r){
21         int mid=(l+r)>>1; tot=0; inc(i,mid,n)calc[++tot]=hash[i]-hash[i-mid]*mi[mid];
22         sort(calc+1,calc+tot+1); int x=0,y=0;
23         inc(i,1,tot){
   
   if(!x||calc[i]!=calc[x])x=i; y=max(y,i-x+1);}
24         if(y>=k)ans=mid,l=mid+1;else r=mid-1;
25     }
26     printf("%d",ans); return 0;
27 }

 

20161102

转载于:https://www.cnblogs.com/YuanZiming/p/6038743.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108801.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python cv.imread_为什么cv2里没有imread

    python cv.imread_为什么cv2里没有imread为什么使用Python-OpenCV虽然python很强大,而且也有自己的图像处理库PIL,但是相对于OpenCV来讲,它还是弱小很多。跟很多开源软件一样OpenCV也提供了完善的python接口,非常便于调用。OpenCV的稳定版是2.4.8,最新版是3.0,包含了超过2500个算法和函数,几乎任何一个能想到的成熟算法都可以通过调用OpenCV的函数来实现,超级方便。一、需要工具本…

    2022年10月14日
    6
  • SpringDataJpa:JpaRepository增删改查

    SpringDataJpa:JpaRepository增删改查Jpa查询1.JpaRepository简单查询基本查询也分为两种,一种是springdata默认已经实现,一种是根据查询的方法来自动解析成SQL。 预先生成方法 springdatajpa默认预先生成了一些基本的CURD的方法,例如:增、删、改等等继承JpaRepositorypublicinterfaceUserRepositoryextends…

    2022年10月20日
    4
  • utc时间戳转换器_java时间转字符串

    utc时间戳转换器_java时间转字符串StringutcStr=”ThuAug1416:45:37UTC2011″;Datedate=newDate(utcStr);SimpleDateFormatsf=newSimpleDateFormat(“yyyy-MM-dd”);sf.format(date);

    2022年10月3日
    4
  • pip清华源安装_pip3换源

    pip清华源安装_pip3换源pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplebcolzpipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplesome-package注意,simple不能少,是https而不是http设为默认升级pip到最新的版本(>=…

    2025年8月2日
    5
  • vim撤销与反撤销

    vim撤销与反撤销按esc进入一般模式之后,u按键则为撤销上一步操作,ctrl+r则为反撤销操作

    2022年6月17日
    25
  • sublime text3激活码 2021【在线注册码/序列号/破解码】

    sublime text3激活码 2021【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    99

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号