【POJ1743】 Musical Theme (二分+后缀数组)
Musical ThemeDescription
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
0Sample Output
5Hint
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 (二分+后缀数组)的更多相关文章
- POJ-1743 Musical Theme,后缀数组+二分!
Musical Theme 人生第一道后缀数组的题,采用大众化思想姿势极其猥琐. 题意:给你n个 ...
- poj1743 Musical Theme【后缀数组】【二分】
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 35044 Accepted: 11628 D ...
- POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串
A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...
- POJ 1743 Musical Theme 二分+后缀数组
Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...
- poj1743 Musical Theme(后缀数组|后缀自动机)
[题目链接] http://poj.org/problem?id=1743 [题意] 求不可重叠最长重复子串. 2015-11-27 [思路] 1) 据题意处理字符串 ...
- 【POJ1743】Musical Theme(后缀数组)
[POJ1743]Musical Theme(后缀数组) 题面 洛谷,这题是弱化版的,\(O(n^2)dp\)能过 hihoCoder 有一点点区别 POJ 多组数据 题解 要求的是最长不可重叠重复子 ...
- POJ 1743 Musical Theme(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, ...
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- P2743(poj1743) Musical Themes[差分+后缀数组]
P2743 乐曲主题Musical Themes(poj1743) 然后呢这题思路其实还是蛮简单的,只是细节特别多比较恶心,忘记了差分带来的若干疏漏.因为转调的话要保证找到相同主题,只要保证一段内相对 ...
随机推荐
- Android 设计随便说说之简单实践(消息流动)
在上面两篇分别说明了设计中较为简单也是很关键的实践点. 第一模块划分,它是根据每个模块所承载的业务,进行划分,是应用程序一个静态的描述. 第二合理组合,它是是将每个模块调动起来,共同实现业务,是一个准 ...
- SQL Server自动化运维系列 - 多服务器数据收集和性能监控
需求描述 在生产环境中,很多情况下需要采集数据,用以定位问题或者形成基线. 关于SQL Server中的数据采集有着很多种的解决思路,可以采用Trace.Profile.SQLdiag.扩展事件等诸多 ...
- mysql的分区技术(建立分区)
-- mysql建立表分区,使用range方法建立: create table t_range( id int(11), money int(11) unsigned not null, date d ...
- 控制器的跳转-modal与push
一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...
- C#操作redis代码汇总
马上要用redis来改造现有的o2o项目了,在linux下部署了个redis,顺便研究了下代码操作,分享下代码 using System; using System.Collections.Gener ...
- 基于C#的IBM消息队列操作客户端
背景: 做XX项目需要把交易的消息推送给YY系统,技术选型MQ 另:选用MQ原因是为了防止YY系统宕机,无法接受收消息 实现 1.安装IBM WebSphere MQ客户端 2.引用amqmdnet. ...
- css样式继承 第7节
样式继承: <html> <head> <title>样式继承</title> <style type="text/css"& ...
- bzoj2732: [HNOI2012]射箭 半平面交
这题乍一看与半平面交并没有什么卵联系,然而每个靶子都可以转化为两个半平面. scanf("%lf%lf%lf",&x,&ymin,&ymax); 于是乎就有 ...
- Q105971:Converting a Regular GUID to a Compressed GUID
Quote from: http://flexerasoftware.force.com/articles/en_US/INFO/Q105971 Synopsis The Windows Ins ...
- 数位DP入门Ural1057
CF一战让我觉得很疲倦,所以今天感觉很慢. 昨天解D题时候,因为太累,根本连题目都没看,今天看了之后感觉不会做,听闻是数位DP问题. 有某神说过,DP的功力建立在刷过的题上,我真的毫无功力可言. 介绍 ...