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. fedora 23 安装genymotion解决方案

    由于学习android开发,都说genymotion模拟器给力,我就尝试了下,安装过程参考 :但出现这种错误:缺少库 libjpeg.so.8 ,我就各种goole和百度找到库(链接地址),解压之后放 ...

  2. Linux C —— 多线程

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5769417. ...

  3. Unity3D 之UGUI 图片

    这里来降价下Unity3Dl的图片 先创建一个图片 图片的属性 Preserve Aspect -->保持图片的原始宽高比例 Set native Size -->图片原始尺寸 Image ...

  4. C#中 字符串的处理

    3.字符串 1).字符串的不可变性 当你给一个字符串重新赋值之后,老值并没有销毁,而是重新开辟一块空间存储新值. 当程序结束后,GC扫描整个内存,如果发现有的空间没有被指向,则立即把它销毁. 2).我 ...

  5. linux/windows系统oracle数据库简单冷备同步

    linux/windows系统oracle数据库简单冷备同步 我们有一个财务系统比较看重财务数据的安全性,同时我们拥有两套系统,一个生产环境(linux),一个应急备份环境(windows).备份环境 ...

  6. linux - Mysql 创建用户和授权

    CREATE USER 'cui'@'%' IDENTIFIED BY 'xxxxxxxxxxxxxxxxxx'; GRANT ALL ON test_db.* TO 'cui'@'%'; REVOK ...

  7. demo——06弹性和制作骰子

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. VS建立可供外部调用的MFC类DLL,C#调用MFC调用

    建立MFC DLL工程.一般选共享MFC库 关键是在你生成的CPP中,添加外部调用的接口 如下,意思是将这个函数对外公开. 如果你希望对外提供类,就把这个方法做成工厂. 如果你希望对外提供MFC的窗体 ...

  9. php练习5——简单的学生管理系统(隐藏控件的使用)

    要求:    程序:gradeManage.html和gradeManage.php          结果       注意:   1.使用隐藏控件时,得在不同表单下,不能在同一个表单下:   2. ...

  10. div+css3实现漂亮的多彩标签云,鼠标移动会有动画

    div+css3实现漂亮的多彩标签云,鼠标移动会有动画 点击运行效果 <style> .dict { margin: 20px 0;clear:both ;text-align:left; ...