Musical Theme

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 28435 Accepted: 9604

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

LouTiancheng@POJ

/*
后缀数组+二分答案.
先将原数组差分.
求出height数组后二分答案判定.
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 20001
using namespace std;
int n,m=188,ans,s[MAXN],sa[MAXN],ht[MAXN],rank[MAXN],t1[MAXN],t2[MAXN],c[190],ch[MAXN];
bool cmp(int *y,int a,int b,int k)
{
int a1=y[a],b1=y[b];
int a2=a+k>=n?-1:y[a+k];
int b2=b+k>=n?-1:y[b+k];
return a1==b1&&a2==b2;
}
void slovesa()
{
int *x=t1,*y=t2;
for(int i=0;i<n;i++) c[x[i]=s[i]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(int k=1,p=0;k<=n;k<<=1,m=p,p=0)
{
for(int i=n-k;i<n;i++) y[p++]=i;
for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y),p=1,x[sa[0]]=0;
for(int i=1;i<n;i++)
{
if(cmp(y,sa[i-1],sa[i],k)) x[sa[i]]=p-1;
else x[sa[i]]=p++;
}
if(p>=n) break;
}
}
void sloveheight()
{
int k=0;
for(int i=0;i<n;i++) rank[sa[i]]=i;
for(int i=0;i<n;ht[rank[i++]]=k)
{
if(!rank[i]) continue;
int j=sa[rank[i]-1];
if(k) k--;
while(j+k<n&&i+k<n&&s[j+k]==s[i+k]) k++;
}
ht[0]=0;
}
bool check(int x)
{
int lans=1e9,rans=0;
for(int i=1;i<n;i++)
{
if(ht[i]>=x)
{
lans=min(lans,min(sa[i],sa[i-1]));
rans=max(rans,max(sa[i],sa[i-1]));
}
else {
if(rans-lans>x) return true;
rans=0;lans=n+1;
}
}
if(rans-lans>x) return true;
return false;
}
void erfen(int l,int r)
{
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
}
void Clear()
{
ans=0,m=188;
memset(t1,0,sizeof t1);
memset(t2,0,sizeof t2);
memset(c,0,sizeof c);
memset(sa,0,sizeof sa);
memset(ht,0,sizeof ht);
memset(rank,0,sizeof rank);
memset(ch,0,sizeof ch);
}
int main()
{
while(~scanf("%d",&n)!=EOF)
{
if(!n) break;Clear();
for(int i=0;i<n;i++) scanf("%d",&ch[i]);
for(int i=0;i<n-1;i++) s[i]=ch[i+1]-ch[i]+89;
slovesa(),
sloveheight(),
erfen(0,n);
if(ans+1>=5) printf("%d\n",ans+1);
else printf("0\n");
}
return 0;
}

Poj 1743 Musical Theme(后缀数组+二分答案)的更多相关文章

  1. Poj 1743 Musical Theme (后缀数组+二分)

    题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...

  2. POJ 1743 Musical Theme ——后缀数组

    [题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...

  3. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  4. [poj 1743] Musical Theme 后缀数组 or hash

    Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...

  5. poj 1743 Musical Theme (后缀数组+二分法)

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

  6. POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )

    题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 :  根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...

  7. POJ.1743.Musical Theme(后缀数组 倍增 二分 / 后缀自动机)

    题目链接 \(Description\) 给定一段数字序列(Ai∈[1,88]),求最长的两个子序列满足: 1.长度至少为5 2.一个子序列可以通过全部加或减同一个数来变成另一个子序列 3.两个子序列 ...

  8. POJ 1743 Musical Theme 后缀数组 不可重叠最长反复子串

    二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include & ...

  9. Poj 3261 Milk Patterns(后缀数组+二分答案)

    Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...

随机推荐

  1. ORACLE数据库实现自增的两种方式

    Mysql数据库因为其有自动+1,故一般我们不需要花费太多时间,直接用关键字auto_increment即可,但是Oracle不行,它没有自动增长机制.顾我们需要自己去实现.一般有两种方式,但是这两种 ...

  2. mpvue + vant + flyio 小程序项目总结

    vant 的使用 我开始是 npm 导入,然后 import,使用不了. 找了各种方法,最后还是下载文件,然后找到 dist 文件夹,复制到项目里,我是放在 static 文件夹,文件名 dist 重 ...

  3. PHP写入文件

    file_put_contents('log.txt',PHP_EOL.'zhangsan'.$time.'查看了数据', FILE_APPEND);

  4. 深入理解JVM(五) -- 垃圾回收算法

    上篇文章我们了解到哪些内存区域和哪些对象可以被回收,这篇文章我们就来了解一下具体的垃圾回收算法的思路,不讨论具体的实现. 一 最基础算法 标记-清除(Mark-Swap) 为什么说他是最基础的算法,因 ...

  5. 【转载】 C#使用string.IsNullOrWhiteSpace方法判断字符串是否为非空字符

    在C#编程过程中,很多时候需要判断传入过来的字符串是否为Null或者空字符或者空白字符,此时就可以使用到string.IsNullOrWhiteSpace方法来判断,如果字符串为null或者空字符Em ...

  6. Springboot项目统一异常处理

    Springboot项目统一异常处理 一.接口返回值封装 1. 定义Result对象,作为通用返回结果封装 2. 定义CodeMsg对象,作为通用状态码和消息封装 二.定义全局异常类 三.定义异常处理 ...

  7. Ane技术大全 - Devil程序员

    来源:http://www.th7.cn/Program/Android/201405/206863.shtml 一.Ane概况 1.Ane是什么? Ane是为了解决 as3(flash)与Java代 ...

  8. RedHat6.9下替换yum源

    因为RedHat的yum需要收费,且要注册后才能使用.因此想把Yum源更新为CentOS的.使用的RedHat版本为6.9,因此对应的CentOS版本也要为6.9 1. 检查并删除原有的yum源 rp ...

  9. WPF登录功能,对于密码框的操作,其实WPF有个PasswordBox专门的密码框控件,完全可以选择自己要显示的密码符号。

    在链接数据库后,点击登录时需要判断用户名和密码框是否为空,而PasswordBox不像textbox那样判断 textbox判断文本框为空 if (this.UserName.Text.Trim()= ...

  10. SQL SERVER-AlwaysonResolving

    虽然2台都是同步提交,自动failover模式 在辅助副本的AG中先删掉一个可用DB,然后关闭主副本,之后辅助副本变为下图.不能自动failover. 重新开启主副本后,主副本显示一切正常 但是辅助副 ...