Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16757   Accepted: 5739

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
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 20001
int a[N],c[N],d[N],e[N],sa[N],height[N],n,b[N],m;
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da()
{
int i,j,p,*x=c,*y=d,*t;
for(i=;i<m;i++)b[i]=;
for(i=; i<n; i++)b[x[i]=a[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,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<n; i++)e[i]=x[y[i]];
for(i=;i<m;i++)b[i]=;
for(i=; i<n; i++)b[e[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[e[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void callheight()
{
int i,j,k=;
b[]=;
for(i=; i<n; i++)b[sa[i]]=i;
n--;
for(i=; i<n; height[b[i++]]=k)
for(k?k--:,j=sa[b[i]-]; a[i+k]==a[j+k]; k++);
}
bool check(int mid)
{
int ma,mi,i=;
while()
{
while(i<n&&height[i]<mid)i++;
if(i>=n)return ;
mi=ma=sa[i-];
while(i<n&&height[i]>=mid)
{
ma=ma>sa[i]?ma:sa[i];
mi=mi<sa[i]?mi:sa[i];
i++;
}
if(ma-mi>=mid)return ;
}
}
int main()
{
int i,l,r;
while(scanf("%d",&n)&&n)
{
scanf("%d",&a[]);
for(i=; i<n; i++)scanf("%d",&a[i]),a[i-]=a[i]-a[i-]+,m=m<a[i-]?a[i-]:m;
m++;
a[n-]=;
da();
callheight();
l=,r=n/;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid))
l=mid+;
else r=mid-;
}
l=l>?l:;
printf("%d\n",l);
} }

Musical Theme poj1743(后缀数组)的更多相关文章

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

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

  2. POJ-1743 Musical Theme,后缀数组+二分!

                                                        Musical Theme 人生第一道后缀数组的题,采用大众化思想姿势极其猥琐. 题意:给你n个 ...

  3. POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...

  4. poj1743 Musical Theme【后缀数组】【二分】

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

  5. poj1743 Musical Theme(后缀数组|后缀自动机)

      [题目链接] http://poj.org/problem?id=1743     [题意]     求不可重叠最长重复子串.   2015-11-27 [思路] 1)      据题意处理字符串 ...

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

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

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

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

  8. POJ 1743 Musical Theme 二分+后缀数组

    Musical Theme   Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...

  9. POJ 1743 Musical Theme(后缀数组)

    题意:有n个数值,算出相邻两个值的差值,此时有n-1个值的序列,把这序列当做字符串的话,求最长重复子串,且这两个子串不能重叠. 分析:后缀数组解决.先二分答案,把题目变成判定性问题:判断是否存在两个长 ...

随机推荐

  1. 工具类:关于解决数据库中的日期格式,经过response.getWriter().write(json)打到前台日期格式混乱的问题的总结

    经过response.getWriter().write(json)打到前台日期格式混乱的问题的总结 import java.text.SimpleDateFormat;import net.sf.j ...

  2. HashMap 底层算法分析

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp72 Hash算法HashMap使用Hash算法,所以在解剖HashMap ...

  3. distinct和group by 去掉重复数据分析

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp40 用distinct关键字只能过滤查询字段中所有记录相同的(记录集相同) ...

  4. Go数组、切片、映射的原理--简明解析

    数组.切片.映射是Golang的最重要的数据结构,下面是对这3种数据结构的一点个人总结: 一.数组 数组是切片和映射的基础数据结构. 数组是一个长度固定的数据类型,存储着一段具有相同数据类型元素的连续 ...

  5. 第4阶段——制作根文件系统之分析init进程(2)

    本节目标: (1) 了解busybox(init进程和命令都放在busybox中) (2) 创建SI工程,分析busybox源码来知道init进程做了哪些事情 (3)  分析busybox中init进 ...

  6. C语言中复数运算及调用blas,lapack中复数函数进行科学计算

    C语言中常用的数据类型主要int, float ,double ,char 等,但在科学运算中复数扮演着重要角色.这里讲下C语言中的复数运算以及如何调用blas,lapack库中的复数函数来进行科学计 ...

  7. Beta阶段项目复审

    复审人:王李焕 六指神功:http://www.cnblogs.com/teamworkers/ wt.dll:http://www.cnblogs.com/TeamOf/ 六个核桃:http://w ...

  8. 201521123003《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 我们利用Sca ...

  9. 201521123122 《java程序设计》 第四周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 这个思维导图比较简单,详细内容点击此处 2. 书面作业 注释的应用 使用类的注释与方法的注释 ...

  10. python学习笔记1.2

    在python中%的用处是求余数,而不是除数.