[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 ...
随机推荐
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 564 Accepted: ...
- ionic运行测试
http://blog.csdn.net/yucihan/article/details/54631747
- POJ - 1017 贪心训练
Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59725 Accepted: 20273 Descrip ...
- Windows2008 – Task Scheduler – ‘Action “C:\Windows\SYSTEM32\cmd.exe” with return code 1’
Remediation Edit Task Let us make the necessary changes, which is to specify the Start folder. Here ...
- java属性为什么没多态,而是方法多态
定义 java多肽的特性:方法具有多态性,属性却没有. 准备 基类: 子类: 测试类: 结果: 分析如下 父类 a=new 子类,实际对象时子类.由于向上转型,我们可以用父类在编译期间代替子类,使得编 ...
- HDU 5878---预处理+二分查找
给一个数n,让你求一个大于等于n的最小的满足题意中2^a*3^b*5^c*7^d的数字. 思路: #include<iostream> #include<cstdio> #in ...
- IDEA无法编译java8的lambda表达式提示Error:(16, 48) java: -source 1.5 中不支持 lambda 表达式
在idea中新建了一个java8的项目,但是写lambda表达式提示语法错误,提示如下错误信息: Error:(16, 48) java: -source 1.5 中不支持 lambda 表达式 (请 ...
- [BZOJ3098]Hash Killer II解题报告
这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题:给你一个长度为N的字符串S,求有多少个不同的长度为L的子串.子串的定义是S[l].S[l + 1].... S[r]这样连续的一段.两 ...
- bzoj 3223 裸splay
裸的splay 今儿写的splay,由于自己刚开始学,发现几个容易漏掉的地方 1:开始给所有的儿子赋值为-1 2:给max[-1]赋值为-maxlongint 3:开始father[root]:=sr ...
- Cpython解释器支持的进程与线程
一.理论部分 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的 ...