Musical Theme

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

【分析】

  二分答案,根据二分出来的答案分组,判断组内最小和最大位置的差是否大于长度即可。

代码如下:(以前的代码)

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; int sa[],rank[],y[],Rsort[];
int wr[],a[],height[],n; bool cmp(int k1,int k2,int ln){return wr[k1]==wr[k2] && wr[k1+ln]==wr[k2+ln];} void get_sa(int m)
{
int i,k,p,ln; memcpy(rank,a,sizeof(rank)); memset(Rsort,,sizeof(Rsort));
for (i=;i<=n;i++) Rsort[rank[i]]++;
for (i=;i<=m;i++) Rsort[i]+=Rsort[i-];
for (i=n;i>=;i--) sa[Rsort[rank[i]]--]=i; ln=; p=;
while (p<n)
{
for (k=,i=n-ln+;i<=n;i++) y[++k]=i;
for (i=;i<=n;i++) if (sa[i]>ln) y[++k]=sa[i]-ln;
for (i=;i<=n;i++) wr[i]=rank[y[i]]; memset(Rsort,,sizeof(Rsort));
for (i=;i<=n;i++) Rsort[wr[i]]++;
for (i=;i<=m;i++) Rsort[i]+=Rsort[i-];
for (i=n;i>=;i--) sa[Rsort[wr[i]]--]=y[i]; memcpy(wr,rank,sizeof(wr));
p=; rank[sa[]]=;
for (i=;i<=n;i++)
{
if (!cmp(sa[i],sa[i-],ln)) p++;
rank[sa[i]]=p;
}
m=p; ln*=;
}
a[]=sa[]=;
} void get_he()
{
int i,j,k=;
for (i=;i<=n;i++)
{
j=sa[rank[i]-];
if (k) k--; while (a[j+k]==a[i+k]) k++;
height[rank[i]]=k;
}
} bool check(int k)
{
int i,maxx=,minn=;
for(i=;i<=n;i++)
{
if(height[i]<k) maxx=minn=sa[i];
else
{
if(sa[i]>maxx) maxx=sa[i];
if(sa[i]<minn) minn=sa[i];
if(maxx-minn>k) return ;
}
}
return ;
} int hd_work()
{
int l,r,mid,ans=;
l=;r=n;
while(l<=r)
{
mid=(l+r)/;
if(check(mid))
{
l=mid+;
ans=mid;
}
else r=mid-;
}
if(ans>=) ans++;
else ans=;
return ans;
} int main()
{
int i,a1,a2;
while()
{
scanf("%d",&n);
if(n==) break;
scanf("%d",&a1);
for(i=;i<=n;i++)
{
scanf("%d",&a2);
a[i-]=a2-a1+;
a1=a2;//做差
}
n--;
get_sa();
get_he();
printf("%d\n",hd_work());
}
}

[POJ1743]

2016-07-20 15:31:18

【POJ1743】 Musical Theme (二分+后缀数组)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. POJ 1743 Musical Theme(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, ...

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

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

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

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

  10. P2743(poj1743) Musical Themes[差分+后缀数组]

    P2743 乐曲主题Musical Themes(poj1743) 然后呢这题思路其实还是蛮简单的,只是细节特别多比较恶心,忘记了差分带来的若干疏漏.因为转调的话要保证找到相同主题,只要保证一段内相对 ...

随机推荐

  1. java中判断Object对象类型

    记录一下 Object param = params.get(i); if (param instanceof Integer) { int value = ((Integer) param).int ...

  2. Oracle+FluentData+MVC4+EasyUI开发权限管理系统之开篇

    在园子里有很多EF+MVC+EasyUI的框架实在是太多了,经过在一段时间的学习高手写的思路,但是都是针对Sql数据的,但是今年我当上研发组组长的第一个任务就是编写一个通用平台框架,一刚开始想把学习过 ...

  3. JavaScript之call()&apply()

    场景一:定义了一个类A,给它一个getName的方法:定义了一个类B,给它一个setName的方法:之前A只需要获取它的Name,B也只需要设置它的Name,但现在有新的需求,A和B都需要设置和获取他 ...

  4. Ext.Net学习笔记02:Ext.Net用法概览

    这两天越来越觉得Ext.Net很强大,如果运用熟练可以极大的提高编程效率.如果你也要学习Ext.Net,推荐你看一下<Ext.Net Web 应用程序开发教程>. Ext.Net与ExtJ ...

  5. OC与Swift的区别五(函数)

    13 函数 oc函数定义: 返回值类型 函数名(参数类型 参数名,参数类型 参数名){ } swift 函数定义: func 函数名(参数名:参数类型,参数名:参数类型) -> 返回值类型{ } ...

  6. 节点的创建--对比jQuery与JavaScript 方法

    一.  创建节点: 节点是DOM结构的基础,根据DOM规范,节点是一个很宽泛的概念,包含元素.属性.文本.文档和注释.但在实际开发中,要动态创建内容,主要操作的节点包括元素.属性和文本. 1.需求:创 ...

  7. 04_过滤器Filter_03_多个Filter的执行顺序

    [Filter链] *在一个web应用中,可以开发编写多个Filter,这些Filter组合起来称为一个Filter链. *web服务器根据Filter在web.xml中的注册顺序,决定先调用哪个Fi ...

  8. 洛谷 U3178 zty的冒险之行

    U3178 zty的冒险之行 题目提供者mangoyang 题目背景 "妈咪妈咪轰"随着一声巨响,zty传送到了Aluba国,在那里浴血奋战,饱读兵书,风餐露宿,吃喝嫖赌,终于到了 ...

  9. 无责任共享 Coursera、Udacity 等课程视频

    本文转载自网络,原作者不详. (本文是用 markdown 写的,访问 https://www.zybuluo.com/illuz/note/71868 获得更佳体验) 程序语言 interactiv ...

  10. 理解Java中的字符串类型

    1.Java内置对字符串的支持: 所谓的内置支持,即不用像C语言通过char指针实现字符串类型,并且Java的字符串编码是符合Unicode编码标准,这也意味着不用像C++那样通过使用string和w ...