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

最新文章

  1. 关于大小型项目如何最大限度提高WebAPi性能
  2. Python 基础之在ubuntu系统下安装双版本python
  3. Java 反射工具类封装
  4. c++引用总结
  5. 使用ajax上传中遇到的问题
  6. 推荐流程设计器http://www.processon.com/
  7. eclipse几个注意的地方
  8. iOS - Swift NSDate 时间
  9. Qt之Concurrent Map和Map-Reduce
  10. 移动端轮播图插件(支持Zepto和jQuery)
  11. 如何使用axis2 构建 Android 服务器后端--- 工具准备与环境配置
  12. BitMap 内存使用优化
  13. eclipse中svn插件的安装
  14. knockout简单实用教程3
  15. 7_Table Views
  16. StackOverFlow的2016统计
  17. MEF初体验之四:Imports声明
  18. MPICH3.2 单机编译、安装及测试
  19. gephi安装后无法打开
  20. java虚拟机 jvm 局部变量表实战

热门文章

  1. Java并发——使用Condition线程间通信
  2. Magento强大的配置系统
  3. C#的语句
  4. XML操作 之获取指定节点值
  5. 今天是程序员节(Programmer‘s Day)
  6. OC中的字符串常用方法
  7. jQuery动态效果实例
  8. Cogs 1008. 贪婪大陆(树状数组)
  9. 查找PHP的配置文件
  10. Extjs发票管理系统