Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22499   Accepted: 7679

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

 
 
题目大意:让你找最长的音乐主题,主题:1.需要最少由5个音符组成 2.音符之间不重叠 3.同一个主题可以是由同时升或降的一段音调组成(如 1 2 3 4 5 10 11 12 13 14 15 是一个音乐主题)。
 
解题思路:相邻的音符之间做差,再加上一个不会让跟差值做和后出现零或者负值的整数,在最后赋值为0,表示结束位置。然后就直接模板套就ok了。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int maxn = 1e5+200;
const int INF = 0x3f3f3f3f;
int a[maxn],s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn];
int rank[maxn], height[maxn];
void build_sa(int n, int m){
int i,*x = t, *y = t2;
//初始化,基数排序
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = s[i]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1){
int p = 0;
for(i = n-k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i]-k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1; x[sa[0]] = 0;
for(i =1; i < n; i++)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] ==y[sa[i]+k] ? p-1:p++;
if(p >= n) break;
m = p;
}
return ;
}
void getheight(int n) { int i, j, k = 0;
for(i = 0; i < n; i++) {
rank[sa[i]] = i;
}
for(i = 0; i < n; i++) {
if(k) k--;
int j = sa[rank[i]-1];
while(s[i+k] == s[j+k]){
k++;
}
height[rank[i]] = k;
}
}
bool check(int mid , int n){
int mi=INF , mx = 0;
for(int i=2;i<=n+1;i++){
if(i==n+1 || height[i] < mid){
// printf("%d %d %d\n",i,height[i],mid);
mi = min(mi, sa[i-1]);
mx = max(mx, sa[i-1]);
if(mx - mi >= mid){
return true;
}
mx = 0;mi = INF;
}
else if(height[i] >= mid){
mi= min(mi,sa[i-1]);
mx= max(mx,sa[i-1]);
}
}
return false;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF && n ){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
if(n<10){
puts("0");
continue;
}
for(int i=0;i<n-1;i++){
s[i]=a[i+1]-a[i]+89;
}
s[n-1]=0;
// for(int i=0;i<=n;i++){
// printf("%d ",s[i]);
// }puts("");
build_sa(n,200);
// for(int i=0;i<=n;i++){
// printf("%d %d-+-+-+\n",i,sa[i]);
// }
getheight(n);
// for(int i=0;i<n;i++){
// printf("%d %d------------------\n",i,height[i]);
// }
int l=4,r=n/2+1,mid;
int ans = 0;
while(l<=r){
mid=(l+r)/2;
if(check(mid , n)){
l=mid+1;
ans=max(mid,ans);
}else{
r=mid-1;
}
}
if(ans<4) puts("0");
else printf("%d\n",ans+1);
}
return 0;
} /*
10
1 1 1 1 1 1 1 1 1 1 */

  

Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】的更多相关文章

  1. poj 1743 后缀数组 求最长不重叠重复子串

    题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...

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

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

  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 (后缀数组+二分法)

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

  5. Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...

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

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

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

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

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

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

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

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

随机推荐

  1. Sublime Text3常用插件以及安装方法(实用)【转载】

    https://www.cnblogs.com/liuchaoH/p/6370008.html Package Control组件在线安装 按Ctrl+`调出console(注:避免热键冲突) 粘贴以 ...

  2. qextserialport打不开com10及以上的串口

    需要在portname前添加\\\\.\\这样就可以了!! 例如 QString portname; portname.append("\\\\.\\").append(ui-&g ...

  3. centos6.5安装dubbo管控台教程(四)

    阅读此文之前,需要先安装zookeeper. 阅读文章: http://www.cnblogs.com/duenboa/articles/6665169.html   1. 下载文件 dubbo-ad ...

  4. position应用之相对父元素的定位

    分别添加以下style即可: 父元素position:relative; 子元素position:absolute; right:0px; bottom:0px;

  5. [poj1088]滑雪(二维最长下降子序列)

    解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  6. p1640&bzoj1854 连续攻击游戏(游戏)

    传送门(洛谷) 传送门(bzoj) 题目 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用 ...

  7. 以后尽量不用cin、cout啦

    cout输出有问题(对于double,不同OJ处理的结果不一样),cin读入机制较scanf繁琐.慢!!!!!!!!

  8. pandas中df.ix, df.loc, df.iloc 的使用场景以及区别

    pandas中df.ix, df.loc, df.iloc 的使用场景以及区别: https://stackoverflow.com/questions/31593201/pandas-iloc-vs ...

  9. webpack@3.6.0(3)-- 优化

    本篇内容 babel配置 打包调试 第三方资源引入 静态资源的集中输出 babel配置 cnpm i -D babel-core babel-loader babel-preset-es2015 // ...

  10. Perfect service(树形dp)

    Perfect service(树形dp) 有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻.求服务器的最小数量.n<=10000. ...