Milk Patterns
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=3261

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

HINT

题意

让你找到最长的可以重复k次的重复子串

题解

后缀数组,跑出height数组之后,然后直接二分然后O(n),check就好了

跑check的时候,只要符合num++就好了,如果大于等于,那就是符合的,然后就好了……

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1005000
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int s[*maxn];
int a[maxn];
int n,k;
int sa[maxn], rank[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], wd[maxn]; int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a+l] == r[b+l];
} void build_sa(int *r, int n, int m){ // 倍增算法 r为待匹配数组 n为总长度 m为字符范围
int i, j, p, *x = wa, *y = wb, *t;
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[i]=r[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[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 < n; i ++) wv[i] = x[y[i]];
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[wv[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i ++){
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p - : p ++;
}
}
} void calHeight(int *r, int n){ // 求height数组。
int i, j, k = ;
for(i = ; i <= n; i ++) rank[sa[i]] = i;
for(i = ; i < n; height[rank[i ++]] = k){
for(k ? k -- : , j = sa[rank[i]-]; r[i+k] == r[j+k]; k ++);
}
}
int check(int mid)
{
int num=;
for(int i=;i<n;i++)
{
if(height[i]>=mid)
num++;
else
num=;
if(num>=k)
return ;
}
return ;
}
int main()
{
n=read(),k=read();
for(int i=;i<n;i++)
a[i]=read(),a[i]++;
a[n++]=;
build_sa(a,n,);
calHeight(a,n);
int l=,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid))
{
l=mid+;
}
else
r=mid-;
}
cout<<r<<endl;
}

POJ 3261 Milk Patterns 可重复k次的最长重复子串的更多相关文章

  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(后缀数组)(k次的最长重复子串)

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

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

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

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

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

  5. 后缀数组 POJ 3261 Milk Patterns

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

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

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

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

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

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

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

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

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

随机推荐

  1. 云计算服务模型,第 2 部分: 平台即服务(PaaS)

    英文原文:Cloud computing service models, Part 2: Platform as a Service 平台即服务 (PaaS) 常常是最容易让人迷惑的云计算类别,因为很 ...

  2. ASP.NET MVC+Bootstrap个人博客之praise.js点赞特效插件(二)

    1. 为啥要做这个点赞插件?    praise.js是一款小巧的jQuery点赞插件,使用简便,效果美观. 在做个人博客时遇到了文章点赞问题.联想到各大社交网络中的点赞特效:手势放大.红心放大等等, ...

  3. HDU 4911 Inversion

    http://acm.hdu.edu.cn/showproblem.php?pid=4911   归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...

  4. 使用Qmake在树莓派上开发Opencv程序

    Qt 安装 PC 端  下载安装即可 https://mirrors.ustc.edu.cn/qtproject/official_releases/qt 树莓派:Qt开发套件和opencv安装sud ...

  5. VS2010下 LibVLC开发环境搭建

    LibVLC环境的搭建  最近又 LIBVLC 做一个视频播放器,封装成ActiveX控件,之前做过一个基于OpenCV的播放器(只解码视频,音频不用,OpenCV也没有解码音频的功能). 到目前位置 ...

  6. 关于“心脏出血”漏洞(heartbleed)的理解

    前阵子“心脏出血”刚发生的时候读了下源代码,给出了自己觉得比较清楚的理解.   -------------------------穿越时空的分割线--------------------------- ...

  7. Spark学习体会

    在去年图计算工作中,和公司里实习的博士生尝试过Spark后,发现Spark比Hadoop在计算速度上后很大的提高.Spark的计算使用Scala语言编写代码,其中图计算用到了GraphX.对Spark ...

  8. bzoj1150

    haha,贪心,边界条件折腾了我一会儿 #include<cstdio> #include<cctype> #include<queue> #include< ...

  9. oracle文件管理OMF

    OMF是为了简化对数据文件的管理,靠参数DB_CREATE_FILE_DEST实现: 如果定义了DB_CREATE_FILE_DEST,则创建表空间就不需要制定数据文件位置.文件名称,数据文件会按照固 ...

  10. Python 核心数据类型

    1.Python中一切皆对象 2.Python中不需要申明对象类型,对象的类型由运行的表达式决定 3.创建了对象意味着绑定了对象的操作到此对象,也就是在固有的对象上只能调用该对象特有的操作.比如只能将 ...