Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 13072   Accepted: 5812
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 Ktimes.

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

又是一道瘠薄题
题目大意:求可重叠k次的最长子串长度,同poj1743,只是二分的时候改成如果同一组内元素个数大于等于K return 1
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 2000005
int num[maxn],ws[maxn],wa[maxn],wb[maxn],sa[maxn];
int rank[maxn],h[maxn],n,wv[maxn],K;
bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m){
int *t,*x=wa,*y=wb,i,j,p;
for (i=;i<m;i++) ws[i]=;
for (i=;i<n;i++) x[i]=r[i];
for (i=;i<n;i++) ws[x[i]]++;
for (i=;i<m;i++) ws[i]+=ws[i-];
for (i=n-;i>=;i--) sa[--ws[x[i]]]=i;
for (j=,p=;p<n;j*=,m=p){
for (p=,i=n-j;i<n;i++) y[p++]=i;
for (i=;i<n;i++) if (sa[i]-j>=) y[p++]=sa[i]-j;
for (i=;i<m;i++) ws[i]=;
for (i=;i<n;i++) wv[i]=x[y[i]];
for (i=;i<n;i++) ws[wv[i]]++;
for (i=;i<m;i++) ws[i]+=ws[i-];
for (i=n-;i>=;i--) sa[--ws[wv[i]]]=y[i];
for (t=x,x=y,y=t,i=,p=,x[sa[]]=;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
}
void cal(int *r,int n){
int i,j,k=;
for (int i=;i<=n;i++) rank[sa[i]]=i;
for (int i=;i<n;h[rank[i++]]=k)
for (k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
}
bool check(int x){
int i=,cnt;
while (){
while (i<=n&&h[i]<x) i++;
if (i>n) break;
cnt=;
while (i<=n&&h[i]>=x){
cnt++;
i++;
}
if (cnt>=K) return ;
}
return ;
}
void work(){
int l=,r=n,ans;
while (l<=r){
int mid=(l+r)/;
if (check(mid)) l=mid+,ans=mid;
else r=mid-;
}
printf("%d\n",ans);
}
int main(){
while (~scanf("%d%d",&n,&K)){
for (int i=;i<n;i++) scanf("%d",&num[i]);
for (int i=;i<n;i++) num[i]++;
num[n]=;
da(num,sa,n+,);
cal(num,n);
work();
}
}
 

poj3261 -- Milk Patterns的更多相关文章

  1. POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-3261 Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Tot ...

  2. POJ-3261 Milk Patterns,后缀数组+二分。。

                                                        Milk Patterns 题意:求可重叠的至少重复出现k次的最长的字串长. 这题的做法和上一题 ...

  3. poj3261 Milk Patterns【后缀数组】【二分】

    Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...

  4. POJ-3261 Milk Patterns(后缀数组)

    题目大意:找出至少出现K次的子串的最长长度. 题目分析:二分枚举长度x,判断有没有最长公共前缀不小于x的并且连续出现了至少k次的有序子串区间. 代码如下: # include<iostream& ...

  5. poj3261 Milk Patterns(后缀数组)

    [题目链接] http://poj.org/problem?id=3261 [题意] 至少出现k次的可重叠最长子串. [思路] 二分长度+划分height,然后判断是否存在一组的数目不小于k即可. 需 ...

  6. poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串

    题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...

  7. POJ3261 Milk Patterns 【后缀数组】

    牛奶模式 时间限制: 5000MS   内存限制: 65536K 提交总数: 16796   接受: 7422 案件时间限制: 2000MS 描述 农夫约翰已经注意到,他的牛奶的质量每天都在变化.经进 ...

  8. POJ3261 Milk Patterns(二分+后缀数组)

    题目求最长的重复k次可重叠子串. 与POJ1743同理. 二分枚举ans判定是否成立 height分组,如果大于等于ans的组里的个数大于等于k-1,这个ans就可行 #include<cstd ...

  9. Milk Patterns poj3261(后缀数组)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9274   Accepted: 4173 Cas ...

随机推荐

  1. CCI_chapter 13C++

    13.9Write a smart pointer (smart_ptr) class template<class T>class SmartPoint{ public: SmartPo ...

  2. Resty 一款极简的restful轻量级的web框架

    https://github.com/Dreampie/Resty Resty 一款极简的restful轻量级的web框架 开发文档 如果你还不是很了解restful,或者认为restful只是一种规 ...

  3. Android WebView简介

    Android的网络功能特别强大,WebView(网络视图)组件支持加载网页,可以理解为使用Webkit内核的浏览器,而它的实现方式有两种: 第一种具体实现步骤如下: (1)在布局文件中先生命WebV ...

  4. hdu4641-K-string(后缀自动机)

    Problem Description Given a string S. K-string is the sub-string of S and it appear in the S at leas ...

  5. Http(get,post)及HttpClient(get,post)的简单使用

    1. 使用 Http 的 Get 方式读取网络数据 import java.io.BufferedReader; import java.io.IOException; import java.io. ...

  6. hdu 5392 Infoplane in Tina Town(数学)

    Problem Description There is a big stone with smooth surface in Tina Town. When people go towards it ...

  7. jdbc调用mysql存储过程实现代码带有输入和输出

    转载自 http://www.jb51.net/article/34747.htm 1. 创建存储过程 建立一个MySQL的存储过程 add_pro 复制代码代码如下: delimiter // dr ...

  8. ZigBee心电传输(二)

    不管怎样,还是在高手的帮助下完成了前面的硬件部分,现在进行ZigBee的心电AD采集和转换.需要把ZigBee重新拾起来. 首先明确下目标和思路吧,目标是将模拟心电信号通过AD转换,变为数字信号,再用 ...

  9. 文本框按键事件onkeydown、onkeypress、onkeyup区别

    当我们在搜索时,会用到这几个事件 onkeydown 是指鼠标按下的那一刻,此时用户不知道按了什么,文本框也不会显示,首先触发的事件 onkeypress 是指鼠标按下然后松开的瞬间,此时仍然获取不到 ...

  10. 做量化模型Matlab、R、Python、F#和C++到底选择哪一个?

    MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室).是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它将数 ...