POJ 3261 Milk Patterns (后缀数组,求可重叠的k次最长重复子串)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 16742 | Accepted: 7390 | |
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
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
/*************************************************************************
> File Name: poj3261.cpp
> Author: LyuCheng
> Created Time: 2017-11-30 17:04
> Description: 询问最长可重复k次的串的长度,求出height数组的值,求连续
的lca是不是大于k如果大于k那么就满足条件
************************************************************************/
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h> #define maxn 23456 using namespace std; struct SuffixArray{
int s[maxn];
int sa[maxn];
int rank[maxn];
int height[maxn];
int t1[maxn],t2[maxn],c[maxn],n;
void build_sa(int m){
int i,*x=t1,*y=t2;
for(i=;i<m;i++)c[i]=;
for(i=;i<n;i++) c[x[i]=s[i]]++;
for(i=;i<m;i++)c[i]+=c[i-];
for(i=n-;i>=;i--) sa[--c[x[i]]]=i;
for(int k=;k<=n;k<<=){
int p=;
for(i=n-k;i<n;i++)y[p++]=i;
for(i=;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=;i<m;i++)c[i]=;
for(i=;i<n;i++) c[x[y[i]]]++;
for(i=;i<m;i++) c[i]+=c[i-];
for(i=n-;i>=;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=,x[sa[]]=;
for(i=;i<n;i++)
x[sa[i]]=y[sa[i]]==y[sa[i-]]&&y[sa[i]+k]==y[sa[i-]+k]?p-:p++;
if(p>=n) break;
m=p;
}
}
void build_height(){
int i,j,k=;
for(i=;i<n;i++) rank[sa[i]]=i;
for(i=;i<n;i++){
if(k)k--;
j=sa[rank[i]-];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
}sa; int n,k;
int maxs;
int ans; inline bool ok(int pos){
int s=;
for(int i=;i<n;i++){
if(sa.height[i]>=pos){
s++;
if(s>=k)
return true;
}else{
s=;
}
}
return false;
} inline void init(){
maxs=-;
ans=;
} int main(){
while(scanf("%d%d",&n,&k)!=EOF){
init();
sa.n=n;
for(int i=;i<n;i++){
scanf("%d",&sa.s[i]);
maxs=max(maxs,sa.s[i]);
}
sa.s[n]=;
sa.build_sa(maxs+);
sa.build_height();
int l=,r=n,m;
while(l<=r){
m=(l+r)>>;
if(ok(m)==true){
ans=m;
l=m+;
}else{
r=m-;
}
}
printf("%d\n",ans);
}
return ;
}
POJ 3261 Milk Patterns (后缀数组,求可重叠的k次最长重复子串)的更多相关文章
- poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串
题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
- poj 3261 求可重叠的k次最长重复子串
题意:求可重叠的k次最长重复子串的长度 链接:点我 和poj1743差不多 #include<cstdio> #include<iostream> #include<al ...
- POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)+后缀数组模板
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7586 Accepted: 3448 Cas ...
- Poj 3261 Milk Patterns(后缀数组+二分答案)
Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...
- poj 3261 Milk Patterns 后缀数组 + 二分
题目链接 题目描述 给定一个字符串,求至少出现 \(k\) 次的最长重复子串,这 \(k\) 个子串可以重叠. 思路 二分 子串长度,据其将 \(h\) 数组 分组,判断是否存在一组其大小 \(\ge ...
- POJ 3261 Milk Patterns(后缀数组+单调队列)
题意 找出出现k次的可重叠的最长子串的长度 题解 用后缀数组. 然后求出heigth数组. 跑单调队列就行了.找出每k个数中最小的数的最大值.就是个滑动窗口啊 (不知道为什么有人写二分,其实写啥都差不 ...
- POJ 3261 Milk Patterns ( 后缀数组 && 出现k次最长可重叠子串长度 )
题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height ...
- poj 3261 后缀数组 可重叠的 k 次最长重复子串
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16430 Accepted: 7252 Ca ...
随机推荐
- .net core mvc 模型绑定 之 json and urlencoded
.net core mvc 模型绑定, FromQuery,对应 url 中的 urlencoded string ("?key1=value1&key2=value2") ...
- 了解Python控制流语句——while 语句
while 语句 Python 中 while 语句能够让你在条件为真的前提下重复执行某块语句. while 语句是 循环(Looping) 语句的一种.while 语句同样可以拥有 else 子句作 ...
- kettle_简单入门
简介 Kettle是一款纯Java开发的ETL工具,它是跨平台的,所以它可以在Window.Linux.Unix上运行.注意什么是ETL,读者可以自行百度了解,我的理解是将一个数据库的数据导入到另外一 ...
- 孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9并使用pydocx模块将结果写入word文档
孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 到今天终于完成了对docx模块针对 ...
- 【转】: 探索Lua5.2内部实现:虚拟机指令(3) Upvalues & Globals
在编译期,如果要访问变量a时,会依照以下的顺序决定变量a的类型: a是当前函数的local变量 a是外层函数的local变量,那么a是当前函数的upvalue a是全局变量 local变量本身就存在于 ...
- python selenium 使用htmlunit 执行测试。非图形界面浏览器。
其实就是换个浏览器,只是这个浏览器没有图形界面而已. browser = webdriver.Chrome() 换成 browser = webdriver.Remote(desired_capabi ...
- OpenMPI源码剖析2:ompi_mpi_errors_are_fatal_comm_handler函数
上一篇文章说道,初始化失败会有一个函数调用: ompi_mpi_errors_are_fatal_comm_handler(NULL, NULL, message); 所以这里简单地进入了 ompi_ ...
- Java进阶知识点:服务端高并发的基石 - NIO与Reactor AIO与Proactor
一.背景 要提升服务器的并发处理能力,通常有两大方向的思路. 1.系统架构层面.比如负载均衡.多级缓存.单元化部署等等. 2.单节点优化层面.比如修复代码级别的性能Bug.JVM参数调优.IO优化等等 ...
- Y460蓝牙键盘无法连接问题解决
mac坏了,无法启动,一直没时间去修理. 近期把大学的时候用的笔记本又翻了出来,小Y,经典的“娱乐本” Y460. Y460上之前被自己各种重装系统,反复从windows到双系统,再到linux之间来 ...
- LeetCode 386——字典序排数
1. 题目 2. 解答 2.1 方法一 假设返回 118 以内数的字典顺序,则为 1,10,100,101,102,...,109,11,110,111,112,...,118,12,13,....根 ...