Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 33462   Accepted: 11124

Description

A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings. 
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 input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes. 
The last test case is followed by one zero. 

Output

For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.

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

Use scanf instead of cin to reduce the read time.

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(后缀数组 二分)的更多相关文章

  1. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

  2. Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...

  3. POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)

    洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...

  4. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  5. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  6. Poj 1743 Musical Theme (后缀数组+二分)

    题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...

  7. POJ-1743 Musical Theme(后缀数组)

    题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...

  8. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  9. POJ 1743 Musical Theme ——后缀数组

    [题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...

随机推荐

  1. 简单读!tomcat源码(一)启动与监听

    tomcat 作为知名的web容器,很棒! 本文简单了从其应用命令开始拆解,让我们对他有清晰的了解,揭开神秘的面纱!(冗长的代码流水线,给你一目了然) 话分两头: 1. tomcat是如何启动的? 2 ...

  2. idea中如何将单个java类导出为jar包文件?

    idea作为一个java开发的便利IDE工具,个人是比较喜欢的,今天来探索个小功能:  导出单个类文件为jar包! 偶有这种需求,就是某个类文件独立存在,但是需要将其导出为jar,供别人临时使用,或者 ...

  3. 原生JS-旋转木马

    原生JS-旋转木马 今天写一个原生JS写的旋转木马JS效果. 实现原理: 1.建立一个数组给每一张图片写对应的z-index,opacity,top,width: 2.实现旋转的操作是把建造的数组里面 ...

  4. 爬虫不过如此(python的Re 、Requests、BeautifulSoup 详细篇)

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. 爬虫的本质就是一段自动抓取互联网信息的程序,从网络获取 ...

  5. django —— KindEditor - 跨域上传图片

    #跨域上传方法 def frontupload(request): if request.method == 'POST': item = {} file = request.FILES.get('i ...

  6. 【翻译】浏览器渲染Rendering那些事:repaint、reflow/relayout、restyle

    原文链接:http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ 有没有被标题中的5个“R”吓到?今天,我们来讨论一下浏览器的渲 ...

  7. Jenkins 忘记admin密码拯救方法

    突然有一日发现自己忘记了jenkins的管理员密码,因为我一直登录的是另外一个非管理员账户.如果出现必须要使用管理员账户操作的,比如用户管理那里的,必须要管理员账号吧,这就尴尬了. 很方的我打开安装j ...

  8. Vue.js + Nuxt.js 项目中使用 Vee-validate 表单校验

    vee-validate 是为 Vue.js 量身打造的表单校验框架,允许您校验输入的内容并显示对应的错误提示信息.它内置了很多常见的校验规则,可以组合使用多种校验规则,大部分场景只需要配置就能实现开 ...

  9. 如何正确的在项目中接入微信JS-SDK

    微信JS-SDK的功能 如果你点进来,那么我相信你应该知道微信的JS-SDK可以用来做什么了.微信的官方文档描述如下. 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. ...

  10. Flink集群部署

    部署方式 一般来讲有三种方式: Local Standalone Flink On Yarn/Mesos/K8s… 单机模式 参考上一篇Flink从入门到放弃(入门篇2)-本地环境搭建&构建第 ...