[USACO06DEC] Milk Patterns
题目描述
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
输入输出样例
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的更多相关文章
- USACO06DEC Milk Patterns——Solution
题目描述 Farmer John has noticed that the quality of milk given by his cows varies from day to day. On f ...
- 解题:USACO06DEC Milk Patterns
题面 初见SA 用了一个常见的按$height$分组的操作:二分答案,然后按$height$分组,遇到一个$height$小于$mid$的就丢进下一组并更新答案,如果最多的那组不少于$k$个说明可行 ...
- BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...
- luoguP2852 [USACO06DEC]Milk Patterns
题意 显然如果有一个子串出现过\(k\)次,那么它必定是一个至少长为k的后缀序的\(LCP\),求出所有相邻的长为\(k-1\)的\(height\)数组的最小值,在其中取最大值即可 code: #i ...
- [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns
洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
- 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 881 Solved: ...
- POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)+后缀数组模板
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7586 Accepted: 3448 Cas ...
随机推荐
- [NOI2002] 银河英雄传说 (带权并查集)
题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- HDU 多校对抗赛第二场 1010 Swaps and Inversions
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Input操作文件
在HTML表单中,可以上传文件的唯一控件就是<input type="file">. 注意:当一个表单包含<input type="file" ...
- Exceptioninthread"main"java.lang.ClassNotFoundsException的问题
报错如下: Exceptioninthread"main"java.lang.ClassNotFoundsException 大致可以判断出是无法定位到main方法,应该是用mav ...
- CentOs7安装JDK/Tomcat/Git/Gradle
安装Jdk: wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/j ...
- codechef T4 IPC Trainers
IPCTRAIN: 训练营教练题目描述 本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日 程安排有 M 天,每天最多上一节课.第 i 名教练 ...
- 归档普通对象Demo示例程序源代码
源代码下载链接:06-归档普通对象.zip34.2 KB // MJPerson.h // // MJPerson.h // 06-归档普通对象 // // Created by apple o ...
- 类似web风格的 Winform 分页控件
背景 最近做一个Winform的小程序,需要用到分页,由于之前一直在用 TonyPagerForWinForm.dll ,但该库没有源代码,网上找的也不全面,索性就准备自己改造一个.在园子里翻了一下, ...
- mysql 等 null 空值排序
[sqlserver]: sqlserver 认为 null 最小. 升序排列:null 值默认排在最前. 要想排后面,则:order by case when col is null then 1 ...