poj3261 -- 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
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
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的更多相关文章
- POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-3261 Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Tot ...
- POJ-3261 Milk Patterns,后缀数组+二分。。
Milk Patterns 题意:求可重叠的至少重复出现k次的最长的字串长. 这题的做法和上一题 ...
- poj3261 Milk Patterns【后缀数组】【二分】
Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...
- POJ-3261 Milk Patterns(后缀数组)
题目大意:找出至少出现K次的子串的最长长度. 题目分析:二分枚举长度x,判断有没有最长公共前缀不小于x的并且连续出现了至少k次的有序子串区间. 代码如下: # include<iostream& ...
- poj3261 Milk Patterns(后缀数组)
[题目链接] http://poj.org/problem?id=3261 [题意] 至少出现k次的可重叠最长子串. [思路] 二分长度+划分height,然后判断是否存在一组的数目不小于k即可. 需 ...
- poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串
题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...
- POJ3261 Milk Patterns 【后缀数组】
牛奶模式 时间限制: 5000MS 内存限制: 65536K 提交总数: 16796 接受: 7422 案件时间限制: 2000MS 描述 农夫约翰已经注意到,他的牛奶的质量每天都在变化.经进 ...
- POJ3261 Milk Patterns(二分+后缀数组)
题目求最长的重复k次可重叠子串. 与POJ1743同理. 二分枚举ans判定是否成立 height分组,如果大于等于ans的组里的个数大于等于k-1,这个ans就可行 #include<cstd ...
- Milk Patterns poj3261(后缀数组)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9274 Accepted: 4173 Cas ...
随机推荐
- LeetCode_Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- JavsScript的基本特点
1.简单性Javascript是一种脚本语言,它采用小程序段的方式实现编程它同样也是一种解释性语言.2.动态性Javascript是动态的,它可以直接对用户或者客户输入做出相应,无须经过Web服务程序 ...
- SOSP 文档 - Windows Azure 存储:具有强一致性的高可用性云存储服务
之前,我们在第 23 届 ACM操作系统原理研讨会 (SOSP)上发布了一篇文章,其中介绍了 Windows Azure存储的内部详细信息. 您可以在此处找到该文章.此次大会还发布了一段视频讲话( ...
- 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应
http://blog.csdn.net/panjunbiao/article/details/8615880 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应
- Linux磁盘及文件系统管理 2---- 使用fdisk进行磁盘管理
1 FDISK分区工具 1 fsidk是来自IBM的分区工具,支持绝大多数的操作系统,几乎所有的Linux都装有fdisk 2 fdisk是一个支持MBR的分区工具,如果要使用GPT的话我们无法使用f ...
- 使用教程 - BestSync同步软件 - SQL2008R2 数据库定时备份解决方案
需求: 1. 某公司的管理软件,数据库为SQL2008R2.2. 将整个数据库作为一个文件,定时同步到FTP 服务器3. 需要有多个备份,每同步一次,都备份上次的文件到备 ...
- MVC 部分视图
ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control.我们的页面往往会有许多重用的地方,可以进行封装重用. 使用部分视图有以下优点: 1. 可以简写代码. ...
- pyqt例子下拉列表
#!/usr/bin/env python # -*- coding: utf-8 -*- from PyQt4.QtCore import Qt from PyQt4.QtGui import QC ...
- socket用法以及tomcat静态动态页面的加载
一.套接字的使用: 分为以下几步: 1.创建ServerSocket 2.接收客户端的连接 3.读取本地的test.html文件 4.构建数据输出通道 5.发送数据 6.关闭资源 代码参考: pack ...
- 使用android-resource-remover删除项目中无用的资源,减少包的大小
写这篇文章的原因是,一个CSDN的资源链接,Android程序员必备精品资源,在该链接的实用工具集锦中有一个工具吸引了我的注意,那就是android-resource-remover,它的解释是:一个 ...