POJ1743 Musical Theme(后缀数组 二分)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 33462 | Accepted: 11124 |
Description
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it:
- is at least five notes long
- appears (potentially transposed -- see below) again somewhere else in the piece of music
- is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)
Transposed means that a constant positive or negative value is added to every note value in the theme subsequence.
Given a melody, compute the length (number of notes) of the longest theme.
One second time limit for this problem's solutions!
Input
The last test case is followed by one zero.
Output
Sample Input
30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0
Sample Output
5
Hint
Source
题目大意:
给出一个长度为$n$的序列,让你找出最长的相似子串。这里相似定义为两个串每次字符对应的差值相同
Sol:
很显然,我们可以对原序列进行差分,这样如果在原序列中长度为$n$的互不相交的相同的字符串,那么答案为$n + 1$
这是一个经典的问题。首先二分答案,然后对$height$数组分组,若$sa[i] - sa[j] > ans$那么可以更新答案
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > ''){if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
int s[MAXN], sa[MAXN], rak[MAXN], tp[MAXN], tax[MAXN], height[MAXN], P, M;
void Qsort() {
for(int i = ; i <= M; i++) tax[i] = ;
for(int i = ; i <= N; i++) tax[rak[i]]++;
for(int i = ; i <= M; i++) tax[i] += tax[i - ];
for(int i = N; i >= ; i--) sa[ tax[rak[tp[i]]]-- ] = tp[i];
}
void SuffixSort() {
M = ;
for(int i = ; i <= N; i++) rak[i] = s[i], tp[i] = i;
Qsort();
for(int w = , p = ; p < N; M = p, w <<= ) {
p = ;
for(int i = ; i <= w; i++) tp[++p] = N - i + ;
for(int i = ; i <= N; i++) if(sa[i] > w) tp[++p] = sa[i] - w;
Qsort(); swap(tp, rak);
rak[sa[]] = p = ;
for(int i = ; i <= N; i++)
rak[sa[i]] = (tp[sa[i]] == tp[sa[i - ]] && tp[sa[i] + w] == tp[sa[i - ] + w]) ? p : ++p; }
int j = , k = ;
for(int i = ; i <= N; i++) {
if(k) k--;
int j = sa[rak[i] - ];
while(s[i + k] == s[j + k]) k++;
height[rak[i]] = k;
}
//for(int i = 1; i <= N; i++) printf("%d ", sa[i]); puts("");
}
bool check(int len) {
int mx = sa[], mi = sa[];
for(int i = ; i <= N; i++) {
if(height[i] >= len - )
mx = max(sa[i], mx),
mi = min(sa[i], mi);
else
mx = mi = sa[i];
if(mx - mi >= len) return ;
}
return ;
}
int solve() {
int l = , r = N, ans = ;
while(l <= r) {
int mid = l + r >> ;
if(check(mid)) l = mid + , ans = mid;
else r = mid - ;
}
return ans;
}
int main() {
while(scanf("%d", &N) && N != ) {
for(int i = ; i <= N; i++) s[i] = read();
for(int i = N; i >= ; i--) s[i] -= s[i - ] - ;
SuffixSort();
int ans = solve();
if(ans >= )
printf("%d\n", ans);
else
printf("%d\n", );
} return ;
}
/*
30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0
*/
POJ1743 Musical Theme(后缀数组 二分)的更多相关文章
- POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Tot ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)
洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- POJ1743 Musical Theme [后缀数组+分组/并查集]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- POJ-1743 Musical Theme(后缀数组)
题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...
- poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)
题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
随机推荐
- Spring中EmptyResultDataAccessException异常产生的原理及处理方法
Spring中EmptyResultDataAccessException异常产生的原理及处理方法 Spring中使用JdbcTemplate的queryForObject方法,当查不到数据时会抛出如 ...
- 第45节:Java当中的变量,面向对象
Java当中的变量 01 Java当中的实例变量和类变量 什么是实例变量和类变量呢? 实例变量为没有用static修饰符修饰的变量,而类变量为用static修饰符修饰的变量. public class ...
- LabVIEW(六):创建VI
1.多使用快捷键,可以提高工作效率键盘快捷键 说明对象/动作Shift-单击 选取多个对象:将对象添加到当前选择之中.方向箭头键 将选中的对象每次移动一个像素.Shift-方向箭头键 将选中的对象每次 ...
- HOG算法资源备忘
最近再研究这个算法,找了不少资料来研究,发现这方面的资料好的并不多,今天就把找到的经典的资料做一个汇总,方便后续查阅吧. 一 基本概念和推导: 1 解释最清楚的:中文网站:https://blog.c ...
- node的安装及基本使用!
一.node的基本安装和使用 1.什么是node.js 答:node.js是JavaScript运行环境.实际上它是Google V8引擎进行了封装.V8引擎执行JavaScript的速度非常的 快 ...
- pycharm安装plugins
1 . 启动 PyCharm 软件 2 . 打开 设置(Setting) 窗口.(操作:File -> Setting 或者 按快捷键 Ctrl + Alt + S) 3 . 在搜索框里面搜索: ...
- 使用C语言实现一个自动刷弹幕的程序
本文使用两种方式来进行刷弹幕操作 1 模拟键盘输入,自动输入文字,然后点击回车. 2 操作剪切板,直接将剪切板的文字粘贴到输入框,然后回车. 模拟键盘输入 如果要输入"弹幕"这两个 ...
- 《HelloGitHub月刊》第 03 期
<HelloGithub>第03期 兴趣是最好的老师,而<HelloGitHub> 就是帮你找到兴趣! 因为我比较熟悉python语言,所以月刊中python语言的项目居多,个 ...
- SLG手游Java服务器的设计与开发——架构分析
微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...
- 【干货】Jquery.Datables与Bootstrap3的组合使用
官方地址 datatables官方网址:www.datatables.net 下载bootstrap3与datables文件包 引用文件 css:bootstrap.css.dataTables.bo ...