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. requireJs压缩合并路径问题

    描述 随着前端开发的重要性,以及业务的复杂性,前端的模块化开发也被大众所接收,最常见的js框架requireJs,一个js文件对应一个模块,方便开发人员调试与维护,但是一个文件对应一个模块增加了htt ...

  2. Java实现非法访问异常

    创建ExceptionTest类,在该类的main()方法中,使用反射获得String类的所有域,不要使用setAccessible方法修改这些域的可见性,然后通过反射获得私有域中与”hash”相匹配 ...

  3. ASP.NET MVC 4 (二)控制器

    MVC中控制器负责处理请求,由它操作数据模型,最后返回视图给用户. IController接口 所有的控制器类以Controller结尾,必须实现System.Web.Mvc.IController接 ...

  4. 解决ora-01034和ora-27101错误

    使用plsql登录oracle数据库,提示如下错误: 定位原因:tnsnames.ora文件中数据库的配置参数有误所致 解决办法:将SERVICE_NAME修改为SID即可

  5. java.lang.IllegalArgumentException: No converter found for return value of type

    原文地址: http://blog.csdn.net/linhaiguo/article/details/51554766 问题原因: 请求返回的数据无法转换,需要添加如下配置 解决方法: 1.在po ...

  6. 带有ZLIB_LIBRARY_DEBUG的FindZLIB.cmake文件

    CMake自带的FindZLIB.cmake只有ZLIB_LIBRARY,而没有ZLIB_LIBRARY_DEBUG 将下面的代码保存成FindZLIB.cmake,替换掉D:\Program Fil ...

  7. [Ubuntu] arp-scan - 扫描网络设备

    使用arp-scan扫描所有网络设备信息. 1. 安装arp-scan ifantastic@ubuntu:~$ sudo apt-get install arp-scan 2. 扫描网络所有设备 i ...

  8. Failed to resolve

    一: 把//classpath 'com.android.tools.build:gradle:2.2.2'改成classpath 'com.android.tools.build:gradle:3. ...

  9. Android学习之Gallery

    在Android中,画廊控件Gallery用来显示图片列表,可以用手指直接拖动图片左右移动.Gallery只能水平显示一行,且Gallery列表中的图片会根据不同的拖动情况向左或向右移动,直到显示到最 ...

  10. 【Java知识点专项练习】之 接口和抽象类的区别

    接口和抽象类的区别 接口(interface)可以说成是抽象类的一种特例,接口中的所有方法都必须是抽象的.接口中的方法定义默认为public abstract类型,接口中的成员变量类型默认为publi ...