Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 25348   Accepted: 8546

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
 
题意: 求长度不小于5的,不重叠重复出现的子串。(重复出现不一定值一样,但是可以一个子串加减一些值后相同)。
 
思路:
由于2个串不一定相同,先求出差得数组,因为差一定是一样的。
然后求height[ ],由于答案不能直接确定,我们可以二分答案。
对于每次二分的答案m,我们可以在height数组中查询,如果height[i] >= m,并且该组内的左端点和
右端点的差值 >= m,说明满足要求。
 
 
/*
* Author: sweat123
* Created Time: 2016/6/28 13:57:31
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int wa[MAXN],wb[MAXN],wc[MAXN],n,r[MAXN],Rank[MAXN],sa[MAXN];
void da(int *r,int *sa,int n,int m){
int *x = wa,*y = wb;
for(int i = ; i < m; i++)wc[i] = ;
for(int i = ; i < n; i++)wc[x[i] = r[i]] ++;
for(int i = ; i < m; i++)wc[i] += wc[i-];
for(int i = n - ; i >= ; i--)sa[--wc[x[i]]] = i;
for(int k = ,p = ; p < n; m = p,k <<= ){
p = ;
for(int i = n - k; i < n; i++)y[p++] = i;
for(int i = ; i < n; i++)if(sa[i] >= k)y[p++] = sa[i] - k;
for(int i = ; i < m; i++)wc[i] = ;
for(int i = ; i < n; i++)wc[x[y[i]]] ++;
for(int i = ; i < m; i++)wc[i] += wc[i-];
for(int i = n - ; i >= ; i--)sa[--wc[x[y[i]]]] = y[i];
swap(x,y);
p = ;
x[sa[]] = ;
for(int i = ; i < n; i++)
x[sa[i]] = (y[sa[i-]] == y[sa[i]] && y[sa[i-]+k] == y[sa[i]+k])?p-:p++;
}
}
int height[MAXN];
void calheight(int *r,int *sa,int n){
int k,j;
k = ;
for(int i = ; i <= n; i++)Rank[sa[i]] = i;
for(int i = ; i < n; height[Rank[i++]] = k)
for(k?k--:,j = sa[Rank[i]-]; r[i+k] == r[j+k]; k++);
}
int ok(int m,int n){
int x,y;
x = INF;
y = -INF;
for(int i = ; i <= n; i++){
if(height[i] >= m){
x = min(x,sa[i]);
y = max(y,sa[i]);
if(y - x >= m)return ;
} else{
x = sa[i];
y = sa[i];
}
}
return ;
}
void solve(){
int l,r,m,ans = ;
l = ,r = n;
while(l <= r){
m = (l + r) >> ;
if(ok(m,n)){
ans = m;
l = m + ;
} else{
r = m - ;
}
}
if(ans < )printf("0\n");
else printf("%d\n",ans + );
}
int main(){
while(~scanf("%d",&n)){
if(!n)break;
for(int i = ; i < n; i++){
scanf("%d",&r[i]);
}
for(int i = ; i < n - ; i++){
r[i] = r[i+] - r[i];
}
n -= ;
int maxval,minval;
maxval = -INF;
minval = INF;
for(int i = ; i < n; i++){
maxval = max(maxval,r[i]);
minval = min(minval,r[i]);
}
if(minval <= ){
minval *= -;
minval += ;
for(int i = ; i < n; i++){
r[i] += minval;
}
maxval += minval;
}
r[n] = ;
da(r,sa,n+,maxval+);
calheight(r,sa,n);
solve();
}
return ;
}

poj1743 后缀数组求不可重叠的重复出现的子串最长长度的更多相关文章

  1. poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串

    题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...

  2. poj 1743 二分答案+后缀数组 求不重叠的最长重复子串

    题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3  or  子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...

  3. HDU3518 后缀数组求不可重叠重复出现的不同子串个数

    枚举子串长度,根据height分组,如果本组sa最小值与sa最大值之差超过枚举的长度,则本组对于答案贡献为1. #include <iostream> #include <vecto ...

  4. Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)

    (累了,这题做了很久!) Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8683   Accepted ...

  5. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

  6. 【POJ2774】Long Long Message(后缀数组求Height数组)

    点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...

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

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

  8. poj3261 后缀数组求重复k次可重叠的子串的最长长度

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13669   Accepted: 6041 Ca ...

  9. [Poj1743] [后缀数组论文例题] Musical Theme [后缀数组不可重叠最长重复子串]

    利用后缀数组,先对读入整数处理str[i]=str[i+1]-str[i]+90这样可以避免负数,计算Height数组,二分答案,如果某处H<lim则将H数组分开,最终分成若干块,判断每块中是否 ...

随机推荐

  1. 校园导游系统(C++实现,VC6.0编译,使用EasyX图形库)

    运行效果: 说明: 由于当年还不会使用多线程,所以很多获取用户点击的地方都是使用循环实现的...CPU占用率会比较高. 代码: //校园导游系统.cpp 1 #include <graphics ...

  2. luogu1207双重回文数[usaco1.2]Dual Palindromes

    题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上 ...

  3. There was an internal API error.的解决办法

    1.当安装应用到4s时,偶尔有些程序会报这个错误,There was an internal API error 解决办法如下: 真机运行下,项目在iOS8.0下运行正常,但是一旦换到iOS7.0和7 ...

  4. 4种sql分页

    四种方式实现SQLServer 分页查询 SQLServer 的数据分页: 假设现在有这样的一张表:CREATE TABLE test( id int primary key not null ide ...

  5. js删除数据的几种方法

    js 删除数组几种方法 var arr=['a','b','c']; 若要删除其中的'b',有两种方法: 1.delete方法:delete arr[1] 这种方式数组长度不变,此时arr[1]变为u ...

  6. matlab FDR校正

    http://home.52brain.com/forum.php?mod=viewthread&tid=27066&page=1#pid170857 http://www.mathw ...

  7. silverlight 4 tools for vs2010无法在vs2010 SP1上安装的解决办法

    环境:英文版vs2010 sp1 + vs2013 RC 90天体验版 原来可以正常做silverilght 4 项目开发,今天因为vs2013 RC过了90天体验期,卸载时顺带把Silverlihg ...

  8. Android 的图片异步请求加三级缓存 ACE

    使用xUtils等框架是很方便,但今天要用代码实现bitmapUtils 的功能,很简单, 1 AsyncTask请求一张图片 ####AsyncTask #####AsyncTask是线程池+han ...

  9. Web性能优化-合并js与css,减少请求

    Web性能优化已经是老生常谈的话题了, 不过笔者也一直没放在心上,主要的原因还是项目的用户量以及页面中的js,css文件就那几个,感觉没什么优化的.人总要进步的嘛,最近在被angularjs吸引着,也 ...

  10. 4.5你太黑了,不带这么玩TypeForwardedTo的

    话说最近好不容易把framework 4.0的metadata信息都能全部抽出了,结果换4.5挂了...framework那帮人在4.5里面用了些什么诡异的玩意? 结果一看4.5的部分field用了t ...