[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 ...
随机推荐
- poj 3422 洛谷P2045 K取方格数(方格取数加强版)
Description: 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来 ...
- hdu 1520Anniversary party 树形dp入门
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...
- 仿FLASH的图片轮换效果
css布局代码(test.css): body { background: #ccc;} ul { padding: 0; margin: 0;} li { list-style: none;} im ...
- 使用vue做移动app时,调用摄像头扫描二维码
现在前端技术发展飞快,前端都能做app了,那么项目中,也会遇到调用安卓手机基层的一些功能,比如调用摄像头,完成扫描二维码功能 下面我就为大家讲解一下,我在项目中调用这功能的过程. 首先我们需要一个中间 ...
- java字符串 64位编码
byte[] encodeBase64 = Base64.encodeBase64("到了是是是是".getBytes("UTF-8")); System.ou ...
- 知问前端——按钮UI
按钮(button),可以给生硬的原生按钮或者文本提供更多丰富多彩的外观.它不单单可以设置按钮或文本,还可以设置单选按钮和多选按钮. 使用button按钮 使用button按钮UI的时候,不一定必须是 ...
- bzoj3918 [Baltic2014]Postman
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3918 [题解] 每日至少更一题啊qwq凑任务(迷 明显猜个结论:随便搜环就行了 然后搜环姿势 ...
- 一致性hash算法小结
把服务器的IP或者主机名作为key对2^32求余,余数一定是2^32-1,然后放到(平行映射)0~2^32次方首尾相连的环上. 理想状态下服务器会均匀分布在这个环上,当数据存储时,依然把key对2 ...
- 使用CloudSight API进行图像识别的Python脚本
# -*- coding: utf-8 -*- # @Time : 2018/03/20 17:02 # @Author : cxa # @File : sss.py # @Software: PyC ...
- [ 手记 ] 关于tomcat开机启动设置问题
今天尝试将tomcat设置为开机启动,大家都知道只需要将启动脚本添加到/etc/rc.local下面开机就会自动执行. /usr/local/tomcat8./bin/startup.sh >& ...