poj 1743 Musical Theme (后缀数组+二分法)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16162 | Accepted: 5577 |
Description
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 last test case is followed by one zero.
Output
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
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 ;
}
poj 1743 Musical Theme (后缀数组+二分法)的更多相关文章
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
- POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )
题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
- POJ.1743.Musical Theme(后缀数组 倍增 二分 / 后缀自动机)
题目链接 \(Description\) 给定一段数字序列(Ai∈[1,88]),求最长的两个子序列满足: 1.长度至少为5 2.一个子序列可以通过全部加或减同一个数来变成另一个子序列 3.两个子序列 ...
- POJ 1743 Musical Theme 后缀数组 不可重叠最长反复子串
二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include & ...
- poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树
题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...
随机推荐
- 工具类(为控件设置色值) - iOS
为了便于日常开发效率,因此创建了一些小的工具类便于使用.具体 code 如下:声明: /* 为控件设置色值 */ #import <UIKit/UIKit.h> @interface UI ...
- 阿里云OSS图片上传plupload.js结合jq-weui 图片上传的插件
项目中用到了oss上传,用的plupload,奈何样式上不敢恭维,特别是放在移动端上使用.于是自己把它移植到了jq weui的上传图片组件上. 更改:选择照片后确认即及时上传至oss服务器,不限制上传 ...
- ES6初识-函数扩展
默认值 function test(x,y='world'){ console.log('默认值'); } function test2(...arg){ for(let v of arg){ con ...
- VM12虚拟机安装os x 10.11系统以及注意事项
一.安装步骤 原文链接:https://blog.csdn.net/soachenshui/article/details/49251513 https://blog.csdn.net/soachen ...
- Linux系统崩溃,数据迁移
就在1小时前,处理了件如标题所述的麻烦事儿.吃完午饭,想对此作个总结,一来自己梳理下过程以便后面遇见类似的事可以 快速处理,二来同行的小伙伴们可以探讨下.故事是这样的,公司所在园区物业晚上断电8小时, ...
- idea中自定义设置xml的头文件的内容
因为在idea中新建的xml默认的头文件,有时候并不是我们需要的这时候可以通过自定义来解决. 如搭建hibernate的实体类的映射xml. 首先 fiel→settings出现 如下框框 在上面搜索 ...
- 解答室内定位技术新方向:蓝牙AoA定位,值得了解 ——概念了解
转载搜狐 室内定位一直被炒的非常火的黑科技,也是近年资本追逐的热点,市场上一直有众多宣称可以做到厘米级,米级精度定位的公司,但问题很多,无法大规模商用.近些年有很多人尝试使用蓝牙beacon方式做定位 ...
- 7 定制10MINs首页2
1.添加 <div class="ui basic segment"> <h1 class="ui center aligned header" ...
- sourceInsight *** more bytes are required
现象:用sourceinsight修改的文件无法保存,提示 No enough space to save "XXX", xxx more bytes are required. ...
- chrome 切换到其他网络后重新加载网
chrome 突然提示 “您与 www.google.com 之间的安全连接目前正受到干扰. 请等待几分钟后再尝试重新加载网页,或在切换到其他网络后重新加载网” 查看证书发现 已经过期 解决:同步下 ...