题目描述

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.

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

8 2
1
2
3
2
3
2
3
1 输出样例#1: 
4

(好像二分+HASH也可以做的样子)
求出后缀数组和height之后,我们按height从大到小再排一次序,然后用并查集从前到后合并,如果某次合并后siz>=k了,那么直接输出当前的height即可。
#include<bits/stdc++.h>
#define ll long long
#define maxn 40005
using namespace std;
int cc[maxn],n,m,k,r[maxn];
int sa[maxn],sax[maxn],sz[maxn];
int rank[maxn],rankx[maxn];
int sec[maxn],ans=,p[maxn];
int s[maxn],a[maxn],ky,height[maxn]; inline bool cmp(int x,int y){
return height[x]>height[y];
} int ff(int x){
return (p[x]==x?x:(p[x]=ff(p[x])));
} inline void prework(){
for(int i=;i<n;i++) cc[s[i]]++;
for(int i=;i<=n;i++) cc[i]+=cc[i-];
for(int i=;i<n;i++) sa[cc[s[i]]--]=i;
for(int i=;i<=n;i++){
rank[sa[i]]=i;
if(i>&&s[sa[i]]==s[sa[i-]]) rank[sa[i]]=rank[sa[i-]];
} int l=;
while(l<n){
memset(cc,,sizeof(cc));
for(int i=;i<n;i++) cc[sec[i]=rank[i+l]]++;
for(int i=n-;i>=;i--) cc[i]+=cc[i+];
for(int i=;i<n;i++) sax[cc[sec[i]]--]=i; memset(cc,,sizeof(cc));
for(int i=;i<n;i++) cc[rank[i]]++;
for(int i=;i<=n;i++) cc[i]+=cc[i-];
for(int i=;i<=n;i++) sa[cc[rank[sax[i]]]--]=sax[i]; for(int i=;i<=n;i++){
rankx[sa[i]]=i;
if(i>&&rank[sa[i]]==rank[sa[i-]]&&sec[sa[i]]==sec[sa[i-]]) rankx[sa[i]]=rankx[sa[i-]];
} for(int i=;i<n;i++) rank[i]=rankx[i];
l<<=;
} int now=,j,mx;
for(int i=;i<n;i++){
if(rank[i]==){
now=,height[]=;
continue;
} if(now) now--;
int j=sa[rank[i]-],mx=max(i,j);
while(mx+now<n&&s[i+now]==s[j+now]) now++; height[rank[i]]=now;
} for(int i=;i<=n;i++) r[i]=i,p[i]=i,sz[i]=;
sort(r+,r+n+,cmp); int u,v,fa,fb;
for(int i=;i<=n;i++){
u=r[i],v=r[i]-;
if(!v) continue; fa=ff(u),fb=ff(v);
if(fa!=fb){
p[fa]=fb;
sz[fb]+=sz[fa];
} if(sz[fb]>=k){
printf("%d\n",height[r[i]]);
return;
}
}
} int main(){
scanf("%d%d",&n,&k);
for(int i=;i<n;i++) scanf("%d",s+i),a[i+]=s[i];
sort(a+,a+n+);
ky=unique(a+,a+n+)-a-;
for(int i=;i<n;i++) s[i]=lower_bound(a+,a+n+,s[i])-a;
prework(); return ;
}

[USACO06DEC] Milk Patterns的更多相关文章

  1. USACO06DEC Milk Patterns——Solution

    题目描述 Farmer John has noticed that the quality of milk given by his cows varies from day to day. On f ...

  2. 解题:USACO06DEC Milk Patterns

    题面 初见SA 用了一个常见的按$height$分组的操作:二分答案,然后按$height$分组,遇到一个$height$小于$mid$的就丢进下一组并更新答案,如果最多的那组不少于$k$个说明可行 ...

  3. BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)

    题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...

  4. luoguP2852 [USACO06DEC]Milk Patterns

    题意 显然如果有一个子串出现过\(k\)次,那么它必定是一个至少长为k的后缀序的\(LCP\),求出所有相邻的长为\(k-1\)的\(height\)数组的最小值,在其中取最大值即可 code: #i ...

  5. [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns

    洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...

  6. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

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

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

  8. 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 881  Solved:  ...

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

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

随机推荐

  1. JS Cookie相关操作

    function setCookie(cookieName, cookieValue, expires) { // 设置Cookie function getCookieName(cookieName ...

  2. MySQL使用笔记(三)表的操作

    By francis_hao    Dec 11,2016 表的操作 表的操作有创建表.查看表.删除表和修改表 创建表 创建表之前要在某个数据库中. mysql> create table ta ...

  3. taotao用户登录springMVC拦截器的实现

    在springMVC中写拦截器,只需要两步: 一.写 java 拦截器类,实现 interceptor 拦截器接口. 二.在 springMVC 的xml配置文件中,配置我们创建的拦截器对象及其拦截目 ...

  4. 我之理解---计时器setTimeout 和clearTimeout

    今天在写个图片切换的问题 有动画滞后的问题,才动手去查setTimeout 和clearTimeout.之前写的图片播放器也有类似的问题,有自动start按钮 和stop按钮, 其他都正常,问题出在每 ...

  5. 【数据结构】bzoj3747Kinoman

    Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...

  6. 栈的图文解析 和 对应3种语言的实现(C/C++/Java)【转】

    概要 本章会先对栈的原理进行介绍,然后分别通过C/C++/Java三种语言来演示栈的实现示例.注意:本文所说的栈是数据结构中的栈,而不是内存模型中栈.内容包括:1. 栈的介绍2. 栈的C实现3. 栈的 ...

  7. [codevs3160]最长公共子串解题报告|后缀自动机

    给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 样例就觉得不能更眼熟啊...好像之前用后缀数组做过一次 然后发现后缀自动机真的好好写啊...(当然当时学后缀数组的时候也这么认为... 这 ...

  8. 【洛谷 P1364】医院设置(树的重心)

    树的重心的定义: 树若以某点为根,使得该树最大子树的结点数最小,那么这个点则为该树的重心,一棵树可能有多个重心. 树的重心的性质: 1.树上所有的点到树的重心的距离之和是最短的,如果有多个重心,那么总 ...

  9. bzoj 2142 国家集训队试题 礼物

    问题转化成求C(N,M) mod P p为非素数,那么我们可以将P分解质因数, 也就是 π pi^ci的形式,因为这些pi^ci是互质的,所以我们可以用crt将他们合并 那么问题就转化成了快速求C(N ...

  10. Kuangbin 带你飞 KMP扩展KMP Manacher

    首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...