POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】
题目题目:http://poj.org/problem?id=3261
Milk Patterns
Time Limit: 5000MS |
Memory Limit: 65536K |
|
Total Submissions: 18880 |
Accepted: 8336 |
|
Case Time Limit: 2000MS |
||
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
题意概括:
给出一串长度为 N 的字符串
要求找最长可重叠子串,要求至少出现K次;
解题思路:
二分答案长度,按照height分组(按照排序后的后缀,很明显这样才是最优的)
判断条件就是判断是否有一组里面的元素数量 >= K;
AC code:
#include <set>
#include <map>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
//const int M = 1e6+10;
int M;
int r[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN];
int sa[MAXN]; //index range 1~n value range 0~n-1
int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
} void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *ws = tmp;
for (i = ; i < m; i++) ws[i] = ;
for (i = ; i < n; i++) ws[x[i] = r[i]]++;
for (i = ; i < m; i++) ws[i] += ws[i - ];
for (i = n - ; i >= ; i--) sa[--ws[x[i]]] = i;
for (j = , p = ; p < n; 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 < n; i++) wv[i] = x[y[i]];
for (i = ; i < m; i++) ws[i] = ;
for (i = ; i < n; i++) ws[wv[i]]++;
for (i = ; i < m; i++) ws[i] += ws[i - ];
for (i = n - ; i >= ; i--) sa[--ws[wv[i]]] = y[i];
for (swap(x, y), p = , x[sa[]] = , i = ; i < n; i++)
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
} int Rank[MAXN]; //index range 0~n-1 value range 1~n
int height[MAXN]; //index from 1 (height[1] = 0)
void calheight(int *r, int *sa, int n)
{
int i, j, k = ;
for (i = ; i <= n; ++i) Rank[sa[i]] = i;
for (i = ; i < n; height[Rank[i++]] = k)
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; ++k);
return;
} int N, K;
bool check(int len)
{
int cnt = ;
for(int i = ; i <= N; i++){
if(height[i] < len) cnt = ;
else cnt++;
if(cnt >= K) return true;
}
return false;
} int main()
{
scanf("%d %d", &N, &K);
for(int i = ; i < N; i++){
scanf("%d", &r[i]);
r[i]++;
M = max(M, r[i]);
}
r[N] = ;
da(r, sa, N+, M+);
calheight(r, sa, N);
int ans = ;
int L = , R = N, mid;
while(L <= R){
mid = (L+R)>>;
if(check(mid)){
L = mid+;
ans = max(mid, ans);
}
else R = mid-;
}
printf("%d\n", ans);
return ;
}
POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】的更多相关文章
- POJ 3261 Milk Patterns ( 后缀数组 && 出现k次最长可重叠子串长度 )
题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
- 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\) 次的最长重复子串,这 \(k\) 个子串可以重叠. 思路 二分 子串长度,据其将 \(h\) 数组 分组,判断是否存在一组其大小 \(\ge ...
- poj 1743 后缀数组 最长不重叠子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30941 Accepted: 10336 D ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )
题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
随机推荐
- C# 配置文件操作类
注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...
- php and js to facebook登陆 最佳实践
Facebook Login Flow & Best Practices Best practice for Facebook login flow with the JavaScript S ...
- 述一个程序员的技能:系统安装(win7版)idea配置
idea配置:http://www.phperz.com/article/15/0923/159043.html 作为一名计算机专业出身的程序员,组装电脑和安装系统是基本技能.打造一个安全稳定高效的开 ...
- MyBatis学习(一)---配置文件,Mapper接口和动态SQL
MyBatis MyBatis官方学习网站 http://www.mybatis.org/mybatis-3/zh/index.html 为什么需要MyBatis? Jdbc操作数据库的不足之处 1. ...
- CentOS-Linux系统下安装JDK
一般情况下,Linux系统都有自带的JDK,但不符合开发要求,所以需要卸载,重新安装JDK 步骤1:查看现有安装的JDK版本 命令: rpm -qa | grep -i java 步骤2:卸载已有软件 ...
- CSS(一)sytle
一:CSS语法组成: 选择符 和声明(声明和声明之间用分号隔开) 声明部分:属性和属性值(用冒号链接) 语法:选择符{ 属性1:属性值: 属性2:属性值: } 所有的CSS语句都要放到 ...
- Ubuntu16.04 安装maven
maven是个项目管理工具,在编程领域应用广泛.本文主要讲述如何在ubuntu16.04系统下安装maven. 第一步,去官网下载maven. 第二步,解压到/opt/maven目录. 创建manve ...
- asp.net中label控件设置字体大小
//后台修改label控件字体 protected void Button1_Click(object sender, EventArgs e) { this.Label1.Font.Size = ...
- visual studio 2015通过附加进程调试wcf服务
网站: 打开wcf服务所在的项目 然后调用iis上部署的HLFC(crm)项目的接口就可以进行调试 注意 WCF服务项目要以管理员身份打开,调用wcf服务的项目要在iis中部署并点击调用后才能在附加到 ...
- 微信小程序-view组件
<view class="section"> <view class="section__title">flex-direction: ...