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、重复出现的同一主题不能有公共部分。

思路:后缀数组。求出任意相邻音符的差值,然后把问题转化为不可重叠最长重复子串,用后缀数组来做。先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。

先不考虑重叠,重复子串的长度要大于等于k,也就是一个区间内的height值都大于等于k,当出现height小于k则重新定位。

再来考虑重叠,我们知道了一个区间的height都大于等于k,如果存在两个后缀距离大于k,那么可以肯定存在两个长度为k的子串是相同的,且不重叠。

参考代码:

 //#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
using namespace std;
#define clr(a,val) memset(a,val,sizeof a)
const int maxm=;
int N;
struct SuffixArray{
int s[maxm];
int sa[maxm],height[maxm],rank[maxm],n;
int t[maxm*],t2[maxm*];
long long cnt[maxm];
void build_sa(int m){
int i,*x=t,*y=t2;
for(i=;i<m;i++) cnt[i]=;
for(i=;i<n;i++) cnt[x[i]=s[i]]++;
for(i=;i<m;i++) cnt[i]+=cnt[i-];
for(i=n-;i>=;i--) sa[--cnt[x[i]]]=i;
for(int k=,p=;k<n;k <<=)
{
p=;
for(i=n-k;i<n;i++) y[p++]=i;
for(i=;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=;i<m;i++) cnt[i]=;
for(i=;i<n;i++) cnt[x[y[i]]]++;
for(i=;i<m;i++) cnt[i]+=cnt[i-];
for(i=n-;i>=;i--) sa[--cnt[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(i=;i<n;i++)
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
if(p>=n)
break;
m=p;
}
}
void build_height()
{
int k=;
for(int i=;i<n;i++) rank[sa[i]]=i;
for(int i=;i<n;i++)
{
if(k) k--;
if(!rank[i]) continue;
int j=sa[rank[i]-];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
} SA; bool check(int key)
{
int tMax = SA.sa[];
int tMin = SA.sa[];
for (int i = ; i<=N; ++i)
{
if (SA.height[i] < key) tMax = tMin = SA.sa[i];
else
{
if(SA.sa[i] < tMin) tMin = SA.sa[i];
if(SA.sa[i] > tMax) tMax = SA.sa[i];
if(tMax - tMin > key) return true;
}
}
return false;
}
int main()
{
while(~scanf("%d",&N)&&N)
{
SA.n=N;
int t,k;N--;
scanf("%d",&t);
for(int i=;i<N;++i)
{
scanf("%d",&k);
SA.s[i]=k-t+;
t=k;
}
SA.s[N]=;
SA.build_sa();
SA.build_height();
int L=,R=N/,ans=;
while(L<=R)
{
int mid=L+R>>;
if(check(mid)) ans=mid,L=mid+;
else R=mid-;
}
printf("%d\n",(ans>=? ans+:));
} return ;
}

POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)的更多相关文章

  1. POJ 1743 - Musical Theme 最长不重叠重复子串

    题意:    给出一列数据,问你其中重复的最长连续子串的长度    但是有要求:        1. 长度至少为 5 .        2. 两串可以不相等,但两串每个对应位置的数字相减差值固定 (即 ...

  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(最长重复子串 后缀数组)

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

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

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

  5. URAL 1297 最长回文子串(后缀数组)

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  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 (后缀数组+二分法)

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

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

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

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

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

随机推荐

  1. Mybatis批量事务处理

    /** * 批量提交数据 * @param sqlSessionFactory * @param mybatisSQLId SQL语句在Mapper XML文件中的ID * @param commit ...

  2. java编程思想第四版第十章总结

    1. 内部类的特性 他允许你把一些逻辑相关的类组织在一起. 2. 使用.this 如果你需要在内部类中堆外部类进行应用,可以使用外部类的名字后面加.this.下面展示了如何使用 .this packa ...

  3. ubunit 16 安装pip

    pip是一个用来安装和管理python包的工具.已经内置到python2.7.9和python3.4及其以上的版本里. python2.7执行: sudo apt-get install python ...

  4. CCF-画字符-详细的注释

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  5. LeetCode 5272. 5272. 统计参与通信的服务器 Count Servers that Communicate

    地址 https://leetcode-cn.com/problems/count-servers-that-communicate/ 题目描述这里有一幅服务器分布图,服务器的位置标识在 m * n  ...

  6. 关于.ssh目录下的known_hosts文件的补充

    一.关于.ssh目录下的known_hosts文件的补充 其实一开始是没有注意到的,按照网上的教程一步一步操作,并没有注意到这个文件的生成.直到有一次我试着去查询.ssh目录是否存在时,出现了下面的情 ...

  7. 五分钟了解物联网SIM卡 | 我的物联网成长记10

    [摘要] SIM卡是移动通信中不可或缺的组成部分,在物联网解决方案中,设备移动上网也需要使用SIM卡.那么,SIM卡是什么?SIM卡有几种?各种SIM卡有什么区别?本文将为您答疑解惑. 通信进化史 过 ...

  8. Socket 实现简单的多线程服务器程序

    **********服务器端************* public class ServerSocket{ public static void main(String[] args) throws ...

  9. 数据表与简单java类映射转换

    简单的Java类的定义来源于数据表的结构, 例如:雇员信息表.部门信息表描述的就是雇员或部门的信息, 在实际的开发之中,数据表和简单java类之间的映射关系如下: 1. 数据实体表设计 = 类的定义: ...

  10. JavaScript实战实例剖析——(激励倒计时日历)

    如今JavaScript在前端开发中的地位越来越高,掌握JavaScript的深度往往能决定你职业道路深远,这次通过制作 带着倒计时功能的激励日历的小实例,进一步细致的掌握JavaScript的语法与 ...