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,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
随机推荐
- Github - 修改语言统计
前些日子看到有人提到这个问题,于是自己也试着解决了一番,在此记录下来,希望对大家有帮助. Github中创建一个repository后会出现一个统计使用语言的颜色条. 就是下面这个东西: 似乎很多人遇 ...
- JS实现一位数显示为两位
使用js脚本实现页面一位数字显示为两位,不足两位前面加“0”. function fix(num, length) { return ('' + num).length < length ? ( ...
- 一、cent OS安装配置JDK
到oracle官网下载JDKhttp://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 在cent OS ...
- 撩课-Java每天5道面试题第25天
156.mvc:view-controller有什么作用? 当我们发送一个请求时,如果没有找到对应的mapping 则会对配置文件当中匹配mvc:view-controller 注意点:使用时要添加后 ...
- Oracle数据库基本操作 (六) —— 数据的导出与导入
一.cmd 下登录oracle数据库下的指定用户 方式一:命令行明文连接登录 打开cmd,输入:sqlplus/nolog 输入:conn username/passworld@数据库实例名 方式二: ...
- ThreeJs 选中物体事件
选中物体变红色demo: https://threejs.org/examples/#webgl_raycast_sprite <!DOCTYPE html> <html lang= ...
- LintCode2016年8月22日算法比赛----骰子求和
骰子求和 题目描述 扔n个骰子,向上面的数字之和为 S .给定 Given n,请列出所有可能的 S 值及其相应的概率. 样例 给定n=1,返回 [ [1, 0.17], [2, 0.17], [3, ...
- Activity 四种launchMode
launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的 Activity实例,是否和其他Activity实例公用一个tas ...
- 最好的原型和流程图绘制工具:OmniGraffle_交互设计
原文地址:http://www.shangxueba.com/jingyan/2230668.html 使用哪种原型设计工具"大概是设计师闲聊时出现频率最高的话题之一.据我了解一般以Visi ...
- python 待关注库
Python待关注库 GUI 图形 Tkinter/wxPython/PyGTK/PyQt/PySide Web框架 django/web2py/flask/bottle/tornadoweb/web ...