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

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.

Source

 //800K    204MS    C++    3271B    2013-12-14 11:09:52
/* 题意:
给出一串音律,要求:
1、最少5个数字长; 2、重复出现的时候,必须相对差值一样,比如{ 1 ,2, 3, 22, 10, 11, 12 },1,2,3与10 11 12是一样的韵律。 3、韵律不可重叠。 求最长的一串 后缀数组+二分法:
WA了n次,最后反复对照..原来是DA算法写错了一步...
假设最长的串长度为 mid ,然后求证mid是否存在,这一步要求对height数组有较好的理解
mid用二分法求解 写完后在看别人代码找bug时才发现这是 男人八题 中的一题,默默的开心- */
#include<stdio.h>
#include<string.h>
#define N 20005
int wa[N],wb[N],ws[N],wv[N],wd[N];
int rank[N],height[N];
int Max(int a,int b)
{
return a>b?a:b;
}
int Min(int a,int b)
{
return a<b?a:b;
}
int Abs(int a)
{
return a<?-a:a;
}
void get_height(int *r,int *sa,int n)
{
int i,j,k=;
for(i=;i<=n;i++) rank[sa[i]]=i;
for(i=;i<n;height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
return;
}
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void DA(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=;i<m;i++) ws[i]=;
for(i=;i<n;i++) ws[x[i]=r[i]]++;
for(i=;i<m;i++) ws[i]+=ws[i-];
for(i=n-;i>=;i--) sa[--ws[x[i]]]=i; for(p=,j=;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++) wv[i]=x[y[i]];
for(i=;i<m;i++) ws[i]=;
for(i=;i<n;i++) ws[wv[i]]++;
for(i=;i<m;i++) ws[i]+=ws[i-];
for(i=n-;i>=;i--) sa[--ws[wv[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++;
}
return;
}
int main(void)
{
int n,r[N];
int sa[N];
while(scanf("%d",&n),n)
{
n--;
for(int i=;i<=n;i++) scanf("%d",&r[i]);
if(n<){
puts("");continue;
}
for(int i=;i<n;i++) r[i]=r[i+]-r[i]+;
r[n]=;
DA(r,sa,n+,);
get_height(r,sa,n);
int up=n;
int down=;
int mid;
int ans;
while(up>=down){
int flag=;
mid=(up+down)>>;
for(int i=;i<=n;i++){
if(height[i]>=mid){
for(int j=i-;j>=;j--){
if(Abs(sa[i]-sa[j])>=mid) flag=;
if(height[j]<mid) break;
}
}
if(flag) break;
}
if(flag){
down=mid+;
ans=mid;
}
else up=mid-;
//printf("%d\n",mid);
}
if(ans<) puts("");
else printf("%d\n",ans+);
}
return ;
}

最新文章

  1. java基础知识总结(1)
  2. WIn2003的IIS6解决IE11登录问题。
  3. mac--又发现了一款mac快捷键神器
  4. 【转载】C/C++之回调函数
  5. EF架构随心所欲打造属于你自己的DbModel【转】
  6. csu 10月 月赛 I 题 The Contest
  7. 好用的log
  8. JS动态增加页面上的控件实例
  9. JAVA编码互转(application/x-www-form-urlencoded)
  10. java File类常用方法
  11. Backbone.js 和 Nodejs 的一些共同点搞不清楚
  12. 【Java基础】【21IO(字符流)&amp;字符流其他内容&amp;递归】
  13. delphi 利用 InterlockedCompareExchange 实现主线程维一锁等待
  14. 【HDFS API编程】查看文件块信息
  15. Pycharm永久激活方式
  16. c++选择重载函数
  17. 如何在cmd中集成git
  18. 利用sqlmap注入测试
  19. 【大话QT之十二】基于CTK Plugin Framework的插件版本号动态升级
  20. 自学Aruba6.1-基本网络参数配置(web页面配置)

热门文章

  1. java基础 序列化反序列化流 实现Serializable 接口 自动装载序列号到对象文本文件如修改不能反序列化对象文本,除非自定义long型常量 打印流
  2. FAT32中文版分析+补充(2)
  3. Go单元测试与基准测试
  4. Shell学习——终端打印
  5. ajax原生js及readystate/status
  6. Servlet异步处理和文件上传
  7. form submit 的callback方法
  8. 输入cin对象的用法
  9. 图像的模糊-opencv
  10. [Uva11178]Morley&#39;s Theorem(计算几何)