Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 30941   Accepted: 10336

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

  1. 30
  2. 25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
  3. 82 78 74 70 66 67 64 60 65 80
  4. 0

Sample Output

  1. 5

Hint

Use scanf instead of cin to reduce the read time.

Source

题意:

给出长度是n的数组表示音符,求最长的相同音乐段长度,相同音乐段是指两段的变化是一样的(即差值数组都一样)。

代码:

  1. //求最长不重叠子串,把给出的数组转换成差值的形式。论文题就不多说了。
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. using namespace std;
  6. const int MAXN=;
  7. const int INF=0x7fffffff;
  8. int he[MAXN+],ra[MAXN+],sa[MAXN+],xx[MAXN+],yy[MAXN+],buc[MAXN+];
  9. int s[MAXN+],a[MAXN+];
  10. int len,m;
  11. void get_suf()
  12. {
  13. int *x=xx,*y=yy;
  14. for(int i=;i<m;i++) buc[i]=;
  15. for(int i=;i<len;i++) buc[x[i]=s[i]]++;
  16. for(int i=;i<m;i++) buc[i]+=buc[i-];
  17. for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
  18. for(int k=;k<=len;k<<=){
  19. int p=;
  20. for(int i=len-;i>=len-k;i--) y[p++]=i;
  21. for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
  22. for(int i=;i<m;i++) buc[i]=;
  23. for(int i=;i<len;i++) buc[x[y[i]]]++;
  24. for(int i=;i<m;i++) buc[i]+=buc[i-];
  25. for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
  26. swap(x,y);
  27. p=;x[sa[]]=;
  28. for(int i=;i<len;i++){
  29. if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
  30. x[sa[i]]=p-;
  31. else x[sa[i]]=p++;
  32. }
  33. if(p>=len) break;
  34. m=p;
  35. }
  36. for(int i=;i<len;i++) ra[sa[i]]=i;
  37. int k=;
  38. for(int i=;i<len;i++){
  39. if(ra[i]==) { he[]=; continue; }
  40. if(k) k--;
  41. int j=sa[ra[i]-];
  42. while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
  43. he[ra[i]]=k;
  44. }
  45. }
  46. bool solve(int mid)
  47. {
  48. int min_sa=INF,max_sa=-INF;
  49. for(int i=;i<len;i++){
  50. if(he[i]>=mid){
  51. min_sa=min(min_sa,min(sa[i],sa[i-]));
  52. max_sa=max(max_sa,max(sa[i],sa[i-]));
  53. if(min_sa+mid-<max_sa) return ;
  54. }else{
  55. min_sa=INF;max_sa=-INF;
  56. }
  57. }
  58. return ;
  59. }
  60. int main()
  61. {
  62. int n;
  63. while(scanf("%d",&n)&&n){
  64. for(int i=;i<n;i++) scanf("%d",&a[i]);
  65. for(int i=;i<n;i++) s[i-]=a[i]-a[i-]+;
  66. len=n-;
  67. m=;
  68. get_suf();
  69. int l=,r=len,ans=;
  70. while(l<=r){
  71. int mid=(l+r)>>;
  72. if(solve(mid)) { ans=mid;l=mid+; }
  73. else r=mid-;
  74. }
  75. if(++ans<) ans=;
  76. printf("%d\n",ans);
  77. }
  78. return ;
  79. }

poj 1743 后缀数组 最长不重叠子串的更多相关文章

  1. POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】

    题目题目:http://poj.org/problem?id=3261 Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Subm ...

  2. POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】

    题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Su ...

  3. POJ 1743 (后缀数组+不重叠最长重复子串)

    题目链接: http://poj.org/problem?id=1743 题目大意:楼教主の男人八题orz.一篇钢琴谱,每个旋律的值都在1~88以内.琴谱的某段会变调,也就是说某段的数可以加减一个旋律 ...

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

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

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

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

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

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

  7. POJ 1743 后缀数组

    题目链接:http://poj.org/problem?id=1743 题意:给定一个钢琴的音普序列[值的范围是(1~88)],现在要求找到一个子序列满足 1,长度至少为5 2,序列可以转调,即存在两 ...

  8. POJ 2217 (后缀数组+最长公共子串)

    题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...

  9. POJ 1743 - Musical Theme 最长不重叠重复子串

    题意:    给出一列数据,问你其中重复的最长连续子串的长度    但是有要求:        1. 长度至少为 5 .        2. 两串可以不相等,但两串每个对应位置的数字相减差值固定 (即 ...

随机推荐

  1. python正则表达式re之compile函数解析

    re正则表达式模块还包括一些有用的操作正则表达式的函数.下面主要介绍compile函数. 定义: compile(pattern[,flags] ) 根据包含正则表达式的字符串创建模式对象. 通过py ...

  2. 利用box-shadow制作loading图

    我们见过很多利用css3做的loading图,像下面这种应该是很常见的.通常制作这种loading,我们会一个标签对应一个圆,八个圆就要八个标签.但是这种做法很浪费资源.我们可以只用一个标签,然后利用 ...

  3. 7. I/O复用

    一.I/O复用的特点 能同时监听多个文件描述符 自身是阻塞的 当多个文件描述符同时就绪时,如果不采取额外的措施,程序就只能按顺序依次处理其中的每一个文件描述符 由于其第三个特点,所以服务器程序看起来仍 ...

  4. Android数据储存之SQLiteDatabase 简单增删改查

    SQLiteDatabase 使用 SQLiteDatabase提供如下方法来打开一个文件对应的数据库: openDatabase(String path, SQLiteDatabase.Cursor ...

  5. 经典面试题(一)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯

    1.        有一个整数数组,请求出两两之差绝对值最小的值.记住,只要得出最小值即可,不需要求出是哪两个数.(Microsoft)  方法1:两两作差求绝对值,并取最小,O( n2 ). 方法2 ...

  6. 软工alpha阶段个人总结

    一.个人总结 类别 具体技能和面试问题 现在的回答(大三下) 语言 最拿手的语言之一,代码量是多少? java,代码量在一万行左右 语言 最拿手的语言之二,代码量是多少? C语言,代码量在五千行左右 ...

  7. 《高性能JavaScript》学习笔记(2)——日更中

    我说日更就日更,接着....今天从缓冲布局信息开始啦! -------------------2016-7-22 21:09:12------------------------------- 14. ...

  8. artdialog对话框 三种样式 网址:http://www.planeart.cn/demo/artDialog/_doc/labs.html

    摇头效果 类似与wordpress登录失败后登录框可爱的左右晃动效果 // 2011-07-17 更新 artDialog.fn.shake = function (){ var style = th ...

  9. TDDL实践

    使用入门-数据源配置 数据源配置,tddl的入口,从datasource切入 <bean id="tddlDataSource" class="com.taobao ...

  10. 【C++】构造函数不能是虚函数

    1 虚函数对应一个vtable,这大家都知道,可是这个vtable其实是存储在对象的内存空间的.问题出来了,如果构造函数是虚的,就需要通过 vtable来调用,可是对象还没有实例化,也就是内存空间还没 ...