Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 35044   Accepted: 11628

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

题意:

在一个序列中找这样一个连续的子序列,他的长度大于等于5, 他在总序列中重复出现。【可以是加减一个定值之后的重复出现】。问这样的子序列的最长长度。

不重叠的重复子串。

思路:

首先由于他可以变调,所以其实我们找的是一个序列,序列中的相邻数之间的差值是一个定值。所以我们先把输入的序列预处理成相邻数之间的差值。

然后在求出SA和height。

总思路类似于https://www.cnblogs.com/wyboooo/p/9865584.html,二分答案,不同的是那道题可以重叠,这道题不行。

区别就在于求序列长度的不同。如果height[i]>=mid, 说明存在这样一个长度大于等于mid的串。但是他们可能是重叠的。

找到这个区间里后缀下标的最大最小值,相减就是答案的这个串,如果这个串大于k【没有等于,因为存的是相邻的差,比实际的个数要小1】,说明可行。

还要注意n=1的情况,RE了一次。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 2e4 + ;
int n, k;
char str[maxn];
int s[maxn];
int sa[maxn];
int t1[maxn], t2[maxn], c[maxn];
int rnk[maxn], height[maxn]; void build_sa(int s[], int n, int m)
{
int i, j, p, *x = t1, *y = t2;
for(i = ; i < m; i++)c[i] = ;
for(i = ; i < n; i++)c[x[i] = s[i]]++;
for(i = ; i < m; i++)c[i] += c[i - ];
for(i = n - ; i >= ; i--)sa[--c[x[i]]] = i;
for(j = ; j <= n; j <<= ){
p = ;
for(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++)c[i] = ;
for(i = ; i < n; i++)c[x[y[i]]]++;
for(i = ; i < m; i++)c[i] += c[i - ];
for(i = n - ; i >= ; i--)sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ;
x[sa[]] = ;
for(i = ; i < n; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + j] == y[sa[i] + j] ? p - :p++;
if(p >= n)break;
m = p;
}
} void get_height(int s[], int n)
{
int i, j, k = ;
//cout<<"SA:"<<endl;
for(i = ; i <= n; i++){
//cout<<sa[i]<<endl;
rnk[sa[i]] = i;
}
for(i = ; i <= n; i++){
if(k) k--;
j = sa[rnk[i] - ];
while(s[i + k] == s[j + k])k++;
height[rnk[i]] = k;
}
} bool check(int t)
{
int mmin = sa[], mmax = sa[];
for(int i = ; i <= n; i++){
if(height[i] < t){
mmin = mmax = sa[i];
}
else{
mmin = min(mmin, sa[i]);
mmax = max(mmax, sa[i]);
if(mmax - mmin > t)return true;
}
}
return false;
} int main()
{ while(scanf("%d", &n) != EOF && n){
for(int i = ; i <= n; i++){
scanf("%d", &s[i]);
}
for(int i = n; i >= ; i--){
s[i] = s[i] - s[i - ] + ;
}
for(int i = ; i < n; i++){
s[i] = s[i + ];
} if(n != )s[n] = ;
//n--;
build_sa(s, n + , );
get_height(s, n); int st = , ed = n, ans = -;
while(st <= ed){
int mid = (st + ed) / ;
//cout<<mid<<endl;
if(check(mid)){
st = mid + ;
ans = mid;
}
else{
ed = mid - ;
}
} if(ans < ){
printf("0\n");
}
else{
printf("%d\n", ans + );
} }
return ;
} /*
22
1 2 3 2 1 2 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/

poj1743 Musical Theme【后缀数组】【二分】的更多相关文章

  1. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

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

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

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

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

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

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

  5. POJ1743 Musical Theme [后缀数组]

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. [OpenCV] IplImage and Operation

    IplImage 一.资源 In this chapter, APIs will make U crazy. Good luck! Next, Review Linear Algebra. Ref:  ...

  2. [Algorithm] Deferred Acceptance Algorithm

    约会配对问题 一.立即接受算法: 对于约会的配对,大家都去追自己最心仪的女生.而这个女生面对几位追求者,要立刻做个决定. 被拒绝的男生们调整一下心情,再去追求心中的 No. 2.以此类推. 这样做法有 ...

  3. Nginx Session Sticky

    nginx的粘性session主要通过nginx-sticky-module实现 1 下载 nginx-sticky-module 下载地址:https://code.google.com/p/ngi ...

  4. 5 -- Hibernate的基本用法 --1 1 对象/关系数据库映射(ORM)

    ORM的全称是Object/Relation Mapping ,即对象/关系数据库映射.ORM可理解成一种规范,它概述了这类框架的基本特征:完成面向对象的编程语言到关系数据库的映射.当ORM框架完成映 ...

  5. javascript 作用域、作用域链理解

    JavaScript作用域就是变量和函数的可访问范围. 1.变量作用域 在JavaScript中,变量作用域分为全局作用域和局部作用域. 全局作用域 任何地方都可以定义拥有全局作用域的变量 1.没有用 ...

  6. 新唐ISP操作步骤(转)

    1,电脑上装上“NuMicro_ICP_Programming_Tool_v1.18.5320.zip”:2,把目标板通过SWD口的NU-LINK连接到电脑的USB口上:3,打开桌面的“NuMicro ...

  7. 《C++标准程序库》笔记之三

    本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...

  8. MVC的路由设置【转】

    转,MVC的路由设置. 后端获取路由里面action的参数,函数需要设置相同的参数名称才行. routes.MapRoute( "Default", "{controll ...

  9. C语言内存使用的常见问题及解决之道

    一  前言 本文所讨论的“内存”主要指(静态)数据区.堆区和栈区空间(详细的布局和描述参考<Linux虚拟地址空间布局>一文).数据区内存在程序编译时分配,该内存的生存期为程序的整个运行期 ...

  10. 【框架学习】Nancy 框架

    Nancy 框架 http://liulixiang1988.github.io/nancy-webkuang-jia.html .是一个轻量级用于构建http相应的web框架: .与mvc类似,有自 ...