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