[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 ...
随机推荐
- “CNKI 中国知网 PDF 全文下载”油猴脚本在线安装地址
https://greasyfork.org/zh-CN/scripts/18841-cnki-%E4%B8%AD%E5%9B%BD%E7%9F%A5%E7%BD%91-pdf-%E5%85%A8%E ...
- SICAU-OJ: 第k小
第k小 题意: 给出一个长度不超过5000的字符串,然后让你找出第K小的字串(1<=K<=5).重复的串大小相等. 题解: 这里我们知道某些串的前缀是肯定小于等于其本身的. 那么长度为5的 ...
- angular js的Inline Array Annotation的理解
inline Array annotation的形式是: someModule.controller('MyController', ['$scope', 'greeter', function($s ...
- [HTML]去除li前面的小黑点,和ul、LI部分属性[转]
转摘自:http://blog.csdn.net/cqkxzyi/article/details/7606181 对于很多人用div来做网站时,总会用到,但在显示效果时前面总是会有一个小黑点,这个令很 ...
- 数据结构之DFS与BFS
深度搜索(DFS) and 广度搜索(BFS) 代码如下: #include "stdafx.h" #include<iostream> #include<st ...
- 403 Forbidden 错误
Forbidden You don't have permission to access /a.php on this server. apache昨天调试 httpd.conf 文件:<Di ...
- 【UOJ131/NOI2015D2T2-品酒大会】sam求后缀树
题目链接:http://uoj.ac/problem/131 题意:给出一个字符串,第i个字符对应的值为a[i], 对于i∈[0,n),求最长公共前缀大于等于i的字串对个数,并求这些字符串对开头对应值 ...
- 【Foreign】最大割 [线性基]
最大割 Time Limit: 15 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 6 1 2 1 1 2 1 3 ...
- 【Luogu P3834】可持久化数组(可持久化线段树)
题目链接 可持久化线段树模板题. 这里总结一下可持久化线段树. 可持久化数据结构就是能恢复历史状态的数据结构,比如可持久化\(Trie\),并查集,平衡树. 可持久化数组是最基础的,这里通过可持久化线 ...
- CF 200 div.1 A
2013-10-11 16:45 Rational Resistance time limit per test 1 second memory limit per test 256 megabyte ...