luogu P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组 + Height数组 + 二分答案 + 扫描
题目描述
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
题解:二分答案出最长长度,利用 $Height$ 数组完成判断.
如果当前长度时合法的,说明有一段连续区间的 $Height$ 值都是要大于等于二分出来的答案的.
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in", "r", stdin)
#define maxn 100000
using namespace std;
int n, m, tot;
int arr[maxn], height[maxn], A[maxn];
namespace SA
{
int rk[maxn], tp[maxn], sa[maxn], tax[maxn];
void qsort()
{
for(int i = 0; i <= m ; ++i) tax[i] = 0;
for(int i = 1; i <= n ; ++i) ++tax[rk[i]];
for(int i = 1; i <= m ; ++i) tax[i] += tax[i - 1];
for(int i = n; i >= 1 ; --i) sa[tax[rk[tp[i]]]--] = tp[i];
}
void build()
{
for(int i = 1; i <= n ; ++i) rk[i] = arr[i], tp[i] = i;
qsort();
for(int k = 1; k <= n ; k <<= 1)
{
int p = 0;
for(int i = n - k + 1; i <= n ; ++i) tp[++p] = i;
for(int i = 1; i <= n ; ++i) if(sa[i] > k) tp[++p] = sa[i] - k;
qsort(), swap(rk, tp), rk[sa[1]] = p = 1;
for(int i = 2; i <= n ; ++i)
{
rk[sa[i]] = (tp[sa[i - 1]] == tp[sa[i]] && tp[sa[i - 1] + k] == tp[sa[i] + k]) ? p : ++p;
}
if(n == p) break;
m = p;
}
int k = 0;
for(int i = 1; i <= n ; ++i) rk[sa[i]] = i;
for(int i = 1; i <= n ; ++i)
{
if(k) --k;
int j = sa[rk[i] - 1];
while(arr[i + k] == arr[j + k]) ++k;
height[rk[i]] = k;
}
}
};
bool check(int t)
{
int o = 0;
for(int i = 1; i <= n ; ++i)
{
if(height[i] < t) o = 0;
if(++o >= tot) return true;
}
return false;
}
int main()
{
// setIO("input");
scanf("%d%d",&n,&tot);
for(int i = 1; i <= n ; ++i) scanf("%d",&arr[i]), A[i] = arr[i];
sort(A + 1, A + 1 + n);
for(int i = 1; i <= n ; ++i) arr[i] = lower_bound(A + 1, A + 1 + n, arr[i]) - A, m = max(m, arr[i]);
SA::build();
int l = 1, r = n, mid, ans = 0;
while(l <= r)
{
mid = (l + r) >> 1;
if(check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
printf("%d\n", ans);
return 0;
}
luogu P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组 + Height数组 + 二分答案 + 扫描的更多相关文章
- Luogu P2852 [USACO06DEC]牛奶模式Milk Patterns
题目链接 \(Click\) \(Here\) 水题.利用\(Height\)的性质维护一个单调栈即可. #include <bits/stdc++.h> using namespace ...
- [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns
洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...
- P2852 [USACO06DEC]牛奶模式Milk Patterns
link 这是一道后缀匹配的模板题 我们只需要将height算出来 然后二分一下答案就可以了 #include<cstdio> #include<algorithm> #inc ...
- [Luogu2852][USACO06DEC]牛奶模式Milk Patterns
Luogu 一句话题意 给出一个串,求至少出现了\(K\)次的子串的最长长度. sol 对这个串求后缀数组. 二分最长长度. 如果有\(K\)个不同后缀他们两两的\(lcp\)都\(>=mid\ ...
- 【后缀数组】【LuoguP2852】 [USACO06DEC]牛奶模式Milk Patterns
题目链接 题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". J ...
- [USACO06DEC] 牛奶模式Milk Patterns
题目链接:戳我 我们知道后缀数组的h数组记录的是后缀i和后缀i-1的最长公共前缀长度,后缀的前缀其实就是子串. 因为是可以重复出现的子串,所以我们只要计算哪些h数组的长度大于等于x即可.这一步操作我们 ...
- 洛谷P2852 牛奶模式Milk Patterns [USACO06DEC] 字符串
正解:SA/二分+哈希 解题报告: 传送门! umm像这种子串的问题已经算是比较套路的了,,,?就后缀的公共前缀这样儿的嘛QwQ 所以可以先求个SA 然后现在考虑怎么判断一个长度为d的子串出现了k次? ...
- 2018.07.17 牛奶模式Milk Patterns(二分+hash)
传送门 一道简单的字符串.这里收集了几种经典做法: SAM,不想写. 后缀数组+二分,不想写 后缀数组+单调队列,不想写 hash+二分,for循哈希,天下无敌!于是妥妥的hash 代码如下: #in ...
- BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...
随机推荐
- hdu_1021_Fibonacci Again_201310232237
Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树
二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...
- AngularJS:让submit重新生效
当我们在html中声明了ng-app后,form的submit就会失效,必须通过angularJS来处理.如果这时还是想用普通的方式提交的话,需要修改form标签,如下所示: <form met ...
- HIHO 16 C
树分治.对于一棵子树的根节点,至少有一条边与儿子相连的属于重边.对于一条轻边,它的贡献值是两端子树大小的乘积,所以,重边应该是贡献值最大的一边. 至于要求所有的点,进行深度优先搜索,因为移动一个点只会 ...
- 笔记本光驱位换SSD固态硬盘之硬盘格式化
笔记本光驱位换SSD固态硬盘之硬盘格式化 系列文章: ThinkPad E430c加装内存和SSD固态硬盘 笔记本光驱位换SSD固态硬盘之Ghost克隆原来的系统到SSD固态硬盘分区 概述 加装SSD ...
- Cookie && Session && Token
Cookies Cookie的由来: HTTP 本身是一个无状态的 request/response 协议. server接收一个来自client的request, 处理完以后返回一个response ...
- Android开发趣事记之周期性广告
前些天做了一个应用,由于怕影响用户体验,所以我将广告设定了一下,就是每启动软件8次.就会弹出一次广告. 在上传到应用宝后.竟然得到了这种结果: 看到了吧.无病毒,无广告. 看来审核人员是不会把应用连续 ...
- nginx安装【linux下安装】
nginx下载安装 http://nginx.org/en/download.html 点击右键,复制链接http://nginx.org/download/nginx-1.14.2.tar.gz c ...
- CreateWindowEx和CreateWindow的区别
CreateWindowEx 函数功能:该函数创建一个具有扩展风格的重叠式窗口.弹出式窗口或子窗口,其他与 CreateWindow函数相同.关于创建窗口和其他参数的内容,请参看CreateWindo ...
- IJ-Error:常见错误
ylbtech-IJ-Error:常见错误 1.返回顶部 1. This application has no explicit mapping for /error, so you are seei ...