Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 13249   Accepted: 5894
Case Time Limit: 2000MS

Description

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

Input

Line 1: Two space-separated integers: N and K 
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

二分答案
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 200003
using namespace std; int n,m;
int s[MN],a[MN];
int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=;
bool w[];
inline void gr(int x){
rank[sa[]]=;
for (int i=;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-]]&&s[sa[i]+x]==s[sa[i-]+x])?rank[sa[i-]]:rank[sa[i-]]+;
for (int i=;i<=n;i++) s[i]=rank[i];
}
inline void gv(){memset(v,,sizeof(v));for (int i=;i<=n;i++) v[s[i]]++;for (int i=;i<=2e5;i++)v[i]+=v[i-];}
inline void gsa(){
gv();for (int i=n;i>=;i--) sa[v[s[i]]--]=i;gr();
for (int i=;i<n;i<<=){
gv();for (int j=n;j>=;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
for (int j=n-i+;j<=n;j++) q[v[s[j]]--]=j;
for (int j=;j<=n;j++) sa[j]=q[j];gr(i);
if (rank[sa[n]]==n) return;
}
}
inline void gh(){for (int i=,k=,j;i<=n;h[rank[i++]]=k) for (k?k--:,j=sa[rank[i]-];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]),s[i]=a[i];
gsa();gh();
int l=,r=n,mid,i,j,k,mmh;
while(l<r){
mid=(l+r+)>>;
for (i=,j,k=;i<=n;i=k++){
memset(w,,sizeof(w));
while (h[k]>=mid&&k<=n) k++;
if (k-i>=m) break;
}
if (i<=n) l=mid;else r=mid-;
}
printf("%d\n",l);
}

1668K 94MS G++ 1288B


 

poj 3261的更多相关文章

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

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

  2. POJ 3261 可重叠k次最长重复子串

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13127   Accepted: 5842 Ca ...

  3. poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)

    题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自 ...

  4. POJ 3261 后缀数组

    题目链接:http://poj.org/problem?id=3261 题意:约翰注意到奶牛产奶的之类是不断变化的,虽然他不能预测从当天到下一天的变化情况但是他知道变化是有规律的,牛奶的质量由一个整数 ...

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

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

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

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

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

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

  8. [poj 3261]后缀数组+滑窗最小值

    题目链接:http://poj.org/problem?id=3261 这个是可以交叉的重复串,所以用height就可以了,但是题目说让重复k次以上,也就是直接做一个k-1长度的滑窗最小值,从这些最小 ...

  9. 后缀数组 POJ 3261 Milk Patterns

    题目链接 题意:可重叠的 k 次最长重复子串.给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠. 分析:与POJ 1743做法类似,先二分答案,height数组分段后统计 LC ...

  10. 【poj 3261】Milk Patterns 后缀数组

    Milk Patterns 题意 给出n个数字,以及一个k,求至少出现k次的最长子序列的长度 思路 和poj 1743思路差不多,二分长度,把后缀分成若干组,每组任意后缀公共前缀都>=当前二分的 ...

随机推荐

  1. 页面重绘(repaint)和回流(reflow)

    前言 页面显示到浏览器上的过程: 1.1.生成一个DOM树. 浏览器将获取到的HTML代码解析成1个DOM树,包含了所有标签,包括display:none和动态添加的节点. 1.2.生成样式结构体. ...

  2. KVM 初探

    KVM 是业界最为流行的 Hypervisor,全称是 Kernel-based Virtual Machine.它是作为 Linux kernel 中的一个内核模块而存在,模块名为 kvm.ko,也 ...

  3. NOI2009 植物大战僵尸

    啊一道好题感觉写得挺爽的啊这题这种有一点懵逼然后学了一点东西之后很明朗的感觉真是好!预处理参考 :http://www.cppblog.com/MatoNo1/archive/2014/11/01/1 ...

  4. css3 结构性伪类选择器

    伪类 选择器 类型 说明 备注 E:first-line 伪元素选择器 选择匹配E元素内的第一行文本 E:first-letter 伪元素选择器 选择匹配E元素内的第一个字符 E:before 伪元素 ...

  5. 跟我一起读postgresql源码(七)——Executor(查询执行模块之——数据定义语句的执行)

    1.数据定义语句的执行 数据定义语句(也就是之前我提到的非可优化语句)是一类用于定义数据模式.函数等的功能性语句.不同于元组增删査改的操作,其处理方式是为每一种类型的描述语句调用相应的处理函数. 数据 ...

  6. Python 阿里大于发送手机验证码

    1.安装阿里大于的包 pip install top 2.事例脚本 # -*- coding: utf-8 -*- import top.api appkey = '2353xxxx' secret ...

  7. 简单搭建ES6的环境

    一.兼容情况 说到ECMAScript6,顺便提一下ECMAScript5,先看一下ES5的兼容情况.ES5浏览器支持情况: Opera 11.60:Internet Explorer 9*:Fire ...

  8. mac SecureCRT设置

    参考: http://www.2cto.com/os/201407/320292.html SecureCRT 设置 1)每次登陆都要输入密码: Global Option -> General ...

  9. spring boot 之热部署(三)

    热部署:当发现程序修改时自动启动应用程序. spring boot使用的是spring-boot-devtools是一个为开发者服务的一个模块.其原理用了classLoader 其中一个加载不变的类, ...

  10. css中的 nav ul li ul 与 nav>ul>li 这两种写法的区别

    <nav> <ul id="a"> <li>a1</li> <li>a2</li> <li>a3 ...