POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Description
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.
Input
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
Sample Input
8 2
1
2
3
2
3
2
3
1
Sample Output
4
记得离散化
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 2e5+, mod = 1e9+, inf = 2e9; ///heght[i] 表示 Suffix(sa[i-1])和Suffix(sa[i]) 的最长公共前缀:
///rank[i] 表示 开头为i的后缀的等级:
///sa[i] 表示 排名为i的后缀 的开头位置: int *rank,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
bool cmp(int *r,int a,int b,int l) {
return r[a] == r[b] && r[a+l] == r[b+l];
} void SA(int *r,int *sa,int n,int m) {
int *x=wa,*y=wb,*t;
for(int i=;i<m;++i)wm[i]=;
for(int i=;i<n;++i)wm[x[i]=r[i]]++;
for(int i=;i<m;++i)wm[i]+=wm[i-];
for(int i=n-;i>=;--i)sa[--wm[x[i]]]=i;
for(int i=,j=,p=;p<n;j=j*,m=p){
for(p=,i=n-j;i<n;++i)y[p++]=i;
for(i=;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<m;++i)wm[i]=;
for(i=;i<n;++i)wm[x[y[i]]]++;
for(i=;i<m;++i)wm[i]+=wm[i-];
for(i=n-;i>=;--i)sa[--wm[x[y[i]]]]=y[i];
for(t=x,x=y,y=t,i=p=,x[sa[]]=;i<n;++i) {
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
}
rank=x;
}
void Height(int *r,int *sa,int n) {
for(int i=,j=,k=;i<n;height[rank[i++]]=k)
for(k?--k:,j=sa[rank[i]-];r[i+k] == r[j+k];++k);
} int n,a[N],k;
int check(int x) {
int i = ,cnt;
while() {
while(i <= n && height[i] < x) i++;
if(i > n) break;
cnt = ;
while(i <= n && height[i] >= x) {
cnt++;
i++;
}
if(cnt >= k) {
return ;
}
}
return ;
}
int main() {
while(~scanf("%d%d",&n,&k)) {
for(int i = ; i <= n; ++i) scanf("%d",&a[i]),r[i-] = a[i];
sort(a+,a+n+);
int c = unique(a+,a+n+) - a - ;
for(int i = ; i < n; ++i) r[i] = lower_bound(a+,a+c+,r[i]) - a;
r[n] = ;
SA(r,sa,n+,);
Height(r,sa,n);
int ll = , rr = n,ans = ;
while(ll <= rr) {
int md = (ll + rr) >> ;
if(check(md)) {
ans = md, ll = md + ;
} else rr = md - ;
}
printf("%d\n",ans);
}
return ;
}
POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次的更多相关文章
- poj 3261 Milk Patterns 后缀数组 + 二分
题目链接 题目描述 给定一个字符串,求至少出现 \(k\) 次的最长重复子串,这 \(k\) 个子串可以重叠. 思路 二分 子串长度,据其将 \(h\) 数组 分组,判断是否存在一组其大小 \(\ge ...
- Poj 3261 Milk Patterns(后缀数组+二分答案)
Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...
- POJ 3261 Milk Patterns(后缀数组+单调队列)
题意 找出出现k次的可重叠的最长子串的长度 题解 用后缀数组. 然后求出heigth数组. 跑单调队列就行了.找出每k个数中最小的数的最大值.就是个滑动窗口啊 (不知道为什么有人写二分,其实写啥都差不 ...
- POJ 3261 Milk Patterns ( 后缀数组 && 出现k次最长可重叠子串长度 )
题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height ...
- poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串
题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...
- poj 1743 二分答案+后缀数组 求不重叠的最长重复子串
题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3 or 子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...
- poj 3261 二分答案+后缀数组 求至少出现k次的最长重复子序列
#include "stdio.h" #define maxn 20010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int rank[ma ...
- POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)+后缀数组模板
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7586 Accepted: 3448 Cas ...
- poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7938 Accepted: 3598 Cas ...
随机推荐
- GitHub新手快速入门日常操作流程
GitHub新手快速入门日常操作流程 1. 注册帐号 打开https://github.com/,填写注册信息并提交. 2. 登录帐号 打开https://github.com/login,输入注册的 ...
- mui,css3 querySelector,appendChild,style.display,insertBefore
<script> mui.init({ swipeBack:true //启用右滑关闭功能 }); window.addEventListener('toggle', function(e ...
- bzoj 3110 K大数查询
第一道整体二分,因为只需要知道每个询问区间中比mid大的数有多少个,就可以直接用线段树区间加,区间求和了. #include<iostream> #include<cstdio> ...
- Alpha阶段第六次Scrum Meeting
情况简述 Alpha阶段第六次Scrum Meeting 敏捷开发起始时间 2016/10/27 00:00 敏捷开发终止时间 2016/10/28 00:00 会议基本内容摘要 提出了目前阶段遇到的 ...
- 【整理】Word OpenXML常用标签
一.背景 最近在做关于Word内容自动标引,需要了解Word的底层结构,顺便梳理一下OpenXML的标签含义,方便后续开发,提高对OpenXML标签的查找效率,也是一个熟悉的过程. 二.内容 < ...
- python学习笔记-(十六)python操作mysql
一. mysql安装 1. windows下安装mysql 1.1. 下载源: http://dev.mysql.com/downloads/installer/,请认准对应版本 Windows (x ...
- Fast Member
https://github.com/mgravell/fast-member/ http://stackoverflow.com/questions/6158768/c-sharp-reflecti ...
- thinkphp 3.2与phpexcel
thinkphp版本:3.2 1.在http://phpexcel.codeplex.com/下载最新PHPExcel 2.把Classes目录下的文件(PHPExcel.php和PHPExcel文件 ...
- python 面向对象(类)
面向对象,即是将具备某种共性的事物抽象成一个类(模板),然后再根据类来创建实例对象进行具体的使用. 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写, ...
- linux零基础入门总结
啊,notepad++贴过来怎么对齐格式这么糟糕呢? #root用户 $普通用户 linux命令 清屏clear翻页清屏 CRT中ctrl+L reset"清空 CRT中不起作用 ...