POJ   1743

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.

题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

1.长度至少为5个音符。

2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

3.重复出现的同一主题不能有公共部分。

思路:后缀数组。求出任意相邻音符的差值,最后一个填充0,然后把问题转化为 不可重叠最长重复子串,用后缀数组来做。先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。解决这个问题的关键还是利用 height数组。把排序后的后缀分成若干组,其中每组的后缀之间的height值都不小于k。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i = 0;i < n; i++)
using namespace std;
const int size=,INF=<<;
int rk[size],sa[size],height[size],w[size],wa[size],res[size];
int N;
void getSa (int len,int up) {
int *k = rk,*id = height,*r = res, *cnt = wa;
rep(i,up) cnt[i] = ;
rep(i,len) cnt[k[i] = w[i]]++;
rep(i,up) cnt[i+] += cnt[i];
for(int i = len - ; i >= ; i--) {
sa[--cnt[k[i]]] = i;
}
int d = ,p = ;
while(p < len){
for(int i = len - d; i < len; i++) id[p++] = i;
rep(i,len) if(sa[i] >= d) id[p++] = sa[i] - d;
rep(i,len) r[i] = k[id[i]];
rep(i,up) cnt[i] = ;
rep(i,len) cnt[r[i]]++;
rep(i,up) cnt[i+] += cnt[i];
for(int i = len - ; i >= ; i--) {
sa[--cnt[r[i]]] = id[i];
}
swap(k,r);
p = ;
k[sa[]] = p++;
rep(i,len-) {
if(sa[i]+d < len && sa[i+]+d <len &&r[sa[i]] == r[sa[i+]]&& r[sa[i]+d] == r[sa[i+]+d])
k[sa[i+]] = p - ;
else k[sa[i+]] = p++;
}
if(p >= len) return ;
d *= ,up = p, p = ;
}
} void getHeight(int len) {
rep(i,len) rk[sa[i]] = i;
height[] = ;
for(int i = ,p = ; i < len - ; i++) {
int j = sa[rk[i]-];
while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
p++;
}
height[rk[i]] = p;
p = max(,p - );
}
} int getSuffix(int s[]) {
int len =N,up = ;
for(int i = ; i < len; i++) {
w[i] = s[i];
up = max(up,w[i]);
}
w[len++] = ;
getSa(len,up+);
getHeight(len);
return len;
} bool valid(int len)
{
int i = , ma, mi;
while()
{
while(i <= N && height[i] < len) i ++;
if(i > N) break;
ma = sa[i-];
mi = sa[i-];
while(i <= N && height[i] >= len)
{
ma = max(ma, sa[i]);
mi = min(mi, sa[i]);
i ++;
}
if(ma - mi >= len) return true;
}
return false;
}
int main()
{
int s[size];
while(scanf("%d",&N)!=EOF)
{
if(!N) return ;
for(int i=;i<N;i++)
{
scanf("%d",&s[i]);
}
for(int i=;i<N-;i++)
{
s[i]=s[i+]-s[i]+;
}
s[N-]=;
getSuffix(s);
int low = , high = (N-)/, mid;
while(low < high)
{
mid = (low + high + ) / ;
if(valid(mid)) {
low = mid;
}else {
high = mid - ;
}
}
int ans = low < ? : low + ;
if(N<) ans=;
printf("%d\n", ans);
}
}
 

后缀数组---Musical Theme的更多相关文章

  1. POJ1743 Musical Theme [后缀数组]

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

  2. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)

    永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...

  3. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  4. 【POJ1743】Musical Theme(后缀数组)

    [POJ1743]Musical Theme(后缀数组) 题面 洛谷,这题是弱化版的,\(O(n^2)dp\)能过 hihoCoder 有一点点区别 POJ 多组数据 题解 要求的是最长不可重叠重复子 ...

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

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

  6. POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】

    题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Su ...

  7. poj 1743 Musical Theme(最长重复子串 后缀数组)

    poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...

  8. poj 1743 Musical Theme (后缀数组+二分法)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16162   Accepted: 5577 De ...

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

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

随机推荐

  1. struts2:struts.xml配置文件详解

    1. 几个重要的元素 1.1 package元素 package元素用来配置包.在Struts2框架中,包是一个独立的单位,通过name属性来唯一标识包.还可以通过extends属性让一个包继承另一个 ...

  2. [转载]IE678兼容性前缀区分

    符合一贯简单的风格,只需记住两个符号 _ 和 * 看例子: #main { background:black; *background:red; _background:blue; } 第一句会被所有 ...

  3. 多条件动态LINQ 组合查询

    本文章转载:http://www.cnblogs.com/wangiqngpei557/archive/2013/02/05/2893096.html 参考:http://dotnet.9sssd.c ...

  4. OpenGL es3.0 初始化及渲染

    class FOpenglEs { public: /** * 初始化 OpenGLES3.0 */ bool initOpenGLES30(HWND hwnd) { EGLConfig config ...

  5. SQL设置SQLServer最大连接数查询语句

    设置最大连接数 下面的T-SQL 语句可以配置SQL Server 允许的并发用户连接的最大数目. exec sp_configure 'show advanced options', 1exec s ...

  6. 转:C/C++内存管理详解 堆 栈

    http://chenqx.github.io/2014/09/25/Cpp-Memory-Management/ 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了 ...

  7. centos下配置java环境变量

    一. 需要配置的环境变量1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin ...

  8. Linux高级编程--10.Socket编程

    Linux下的Socket编程大体上包括Tcp Socket.Udp Socket即Raw Socket这三种,其中TCP和UDP方式的Socket编程用于编写应用层的socket程序,是我们用得比较 ...

  9. shell 和awk性能对比

    time for ((i=0;i<10000;i++)) do ((sum+=i)); done real    0m0.086suser    0m0.079ssys    0m0.007s ...

  10. AutoLayout那些坑

    最近在做一个聊天界面,要适配iOS所有屏幕. 以前的思路是键盘弹出的时候去改table 和输入框的frame. 现在发现和autolayout的约束有冲突. 搞了半天发现需要动态改Constraint ...