Milk Patterns

题意:

在一串数字中,求至少连续k次的最大子序列长度;

思路:

二分加哈希;

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std; const int maxn = ;
const int p = ; int n,k,a[maxn+];
unsigned long long hash1[maxn+],F[maxn+]={}; //F数组用来保存p的次方,可能是pow()不稳定,慎用
unsigned long long re[maxn+]; bool judge(int x)
{
memset(re,,sizeof(re));
int tot = ;
for(int i=;i<=n-x+;i++)
{
int r = i+x-;
re[++tot] = (hash1[r]-hash1[i-]*F[r-i+]);
}
sort(re+,re++tot);
int cnt=,mmax=;
unsigned long long last = re[];
for(int i=;i<=tot;i++)
{
if(re[i]==last)
{
cnt++;
if(cnt>mmax)mmax = cnt;
}
else
{
cnt=;
}
last = re[i];
}
return mmax >= k;
} int main(){
scanf("%d%d",&n,&k);
hash1[]=;
F[] = ;
for(int i=;i<=n;i++)
{
F[i] = F[i-]*p;
}
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(i==)hash1[i]=a[i];
else
hash1[i] = hash1[i-]*p +a[i];
}
int le = 1,ri = n;
while(le+<ri)
{
int mid = le + (ri-le)/;
if(judge(mid)) le = mid;
else ri = mid;
}
printf("%d\n",le);
return ;
}

POJ-3261-Milk Patterns-二分+哈希的更多相关文章

  1. POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)+后缀数组模板

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7586   Accepted: 3448 Cas ...

  2. POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】

    题目题目:http://poj.org/problem?id=3261 Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Subm ...

  3. Poj 3261 Milk Patterns(后缀数组+二分答案)

    Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...

  4. POJ 3261 Milk Patterns 可重复k次的最长重复子串

    Milk PatternsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3261 Description ...

  5. POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

    Milk Patterns   Description Farmer John has noticed that the quality of milk given by his cows varie ...

  6. poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7938   Accepted: 3598 Cas ...

  7. POJ 3261 Milk Patterns (后缀数组,求可重叠的k次最长重复子串)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16742   Accepted: 7390 Ca ...

  8. POJ 3261 Milk Patterns(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过 ...

  9. poj 3261 Milk Patterns 后缀数组 + 二分

    题目链接 题目描述 给定一个字符串,求至少出现 \(k\) 次的最长重复子串,这 \(k\) 个子串可以重叠. 思路 二分 子串长度,据其将 \(h\) 数组 分组,判断是否存在一组其大小 \(\ge ...

  10. POJ 3261 Milk Patterns(后缀数组+二分答案+离散化)

    题意:给定一个字符串,求至少出现k 次的最长重复子串,这k 个子串可以重叠. 分析:经典的后缀数组求解题:先二分答案,然后将后缀分成若干组.这里要判断的是有没有一个组的符合要求的后缀个数(height ...

随机推荐

  1. Android平台使用Ceres Solver

    在Android平台上使用Ceres求解器,官方教程不明确,且编译过程遇到了很多问题. 环境 Ubuntu 18.04 源代码 https://github.com/Great-Keith/ceres ...

  2. 在一个含有1-n的序列中,每次找到第Ki小的数,并把它删除(线段树)

    提交链接 Data structure is one of the basic skills for Computer Science students, which is a particular ...

  3. 转载 | CSS图片下面产生间隙的 6种解决方案

    在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对於 该问题的解决方法也是「见机行事」,根据原因的不同要用不同的 ...

  4. CentOS yum 源修改

    修改 CentOS 默认 yum 源为 mirrors.163.com 首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo [root@localhos ...

  5. 使用JMS接口接入WebSphere MQ消息

    在你的应用程序中利用IBM WebSphere MQ消息中间件提供Java消息服务开放接口. IBM WebSphere MQ(WMQ)是一套面向消息的中间件(message-oriented mid ...

  6. gin+vue的前后端分离开源项目

    该项目是gin+vue的前后端分离项目,使用gorm访问MySQL,其中vue前端是使用vue-element-admin框架简单实现的: go后台使用jwt,对API接口进行权限控制.此外,Web页 ...

  7. Hive 系列(五)—— Hive 分区表和分桶表

    一.分区表 1.1 概念 Hive 中的表对应为 HDFS 上的指定目录,在查询数据时候,默认会对全表进行扫描,这样时间和性能的消耗都非常大. 分区为 HDFS 上表目录的子目录,数据按照分区存储在子 ...

  8. hive concat_ws源代码

    其他相关源码可以到以下链接查看: https://github.com/apache/hive/tree/master/ql/src/java/org/apache/hadoop/hive/ql/ud ...

  9. csdn论坛页抓取

    抓取csdn论坛 实现功能 获取论坛分类所有链接,并拼接成推荐精华页的完成的链接 获取推荐精华页的帖子状态,赏分,帖子标题,作者,发布时间,回复量,查看量,最后发表时间 置顶内容不爬取,只打印置顶内容 ...

  10. mybatis 源码分析(四)一二级缓存分析

    本篇博客主要讲了 mybatis 一二级缓存的构成,以及一些容易出错地方的示例分析: 一.mybatis 缓存体系 mybatis 的一二级缓存体系大致如下: 首先当一二级缓存同时开启的时候,首先命中 ...