题目链接:https://vjudge.net/problem/POJ-1743

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 32402   Accepted: 10808

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

题意:

给出一串数字,定义theme为:长度不小于5,从左到右以相同的变化规律出现了不止一次,并且不能重叠。求最长的theme。实际上是求:字符串的重复出现且不重叠的最长子串。

题解:

1.由于求的是变化规律,所以要求出相邻两个数的差值,得到新的一串数字。然后求出新串的后缀数组。

2.二分答案,即“重复出现且不重叠的最长子串”的长度k。然后根据是否存在这样的子串来缩小k的范围,最终得到答案。那么怎样判断是否存在“重复出现且不重叠的长度为k的子串”呢?

2.1 把后缀按名次排成一列,如果前m个后缀(第一名除外)与它的前一名的最长公共前缀都大于等于k(二分时的mid),即height[2~m]>=k,则可以说明这m个后缀的最长公共前缀大于等于k。所以可以得出结论:k把所有后缀分成若干组,并且每一组的最长公共前缀大于等于k(可以单独一个后缀作为一组)。那么,我们只需要判断:是否存在一组后缀,使得max(sa[i]) - min(sa[i]) >= k。

2.2 视图更加直观:

2.3 参考:http://blog.csdn.net/huangzhengdoc/article/details/53573198

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const double EPS = 1e-;
  15. const int INF = 2e9;
  16. const LL LNF = 9e18;
  17. const int MOD = 1e5;
  18. const int MAXN = +;
  19.  
  20. bool cmp(int *r, int a, int b, int l)
  21. {
  22. return r[a]==r[b] && r[a+l]==r[b+l];
  23. }
  24.  
  25. int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
  26. int t1[MAXN], t2[MAXN], c[MAXN];
  27. void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
  28. {
  29. n++;
  30. int i, j, p, *x = t1, *y = t2;
  31. for(i = ; i<m; i++) c[i] = ;
  32. for(i = ; i<n; i++) c[x[i] = str[i]]++;
  33. for(i = ; i<m; i++) c[i] += c[i-];
  34. for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
  35. for(j = ; j<=n; j <<= )
  36. {
  37. p = ;
  38. for(i = n-j; i<n; i++) y[p++] = i;
  39. for(i = ; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j;
  40.  
  41. for(i = ; i<m; i++) c[i] = ;
  42. for(i = ; i<n; i++) c[x[y[i]]]++;
  43. for(i = ; i<m; i++) c[i] += c[i-];
  44. for(i = n-; i>=; i--) sa[--c[x[y[i]]]] = y[i];
  45.  
  46. swap(x, y);
  47. p = ; x[sa[]] = ;
  48. for(i = ; i<n; i++)
  49. x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++;
  50. if(p>=n) break;
  51. m = p;
  52. }
  53.  
  54. int k = ;
  55. n--;
  56. for(i = ; i<=n; i++) Rank[sa[i]] = i;
  57. for(i = ; i<n; i++)
  58. {
  59. if(k) k--;
  60. j = sa[Rank[i]-];
  61. while(str[i+k]==str[j+k]) k++;
  62. height[Rank[i]] = k;
  63. }
  64. }
  65.  
  66. bool test(int mid, int n)
  67. {
  68. int minn = sa[], maxx = sa[];
  69. for(int i = ; i<=n; i++)
  70. {
  71. if(height[i]<mid)
  72. minn = maxx = sa[i];
  73. else
  74. {
  75. maxx = max(maxx, sa[i]);
  76. minn = min(minn, sa[i]);
  77. if(maxx-minn>=mid)
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83.  
  84. int main()
  85. {
  86. int n;
  87. while(scanf("%d",&n)&&n)
  88. {
  89. for(int i = ; i<n; i++) scanf("%d", &r[i]);
  90. for(int i = ; i<n-; i++) r[i] = r[i+]-r[i]+;
  91. r[--n] = ;
  92. DA(r, sa, Rank, height, n, );
  93. int l = , r = n/;
  94. while(l<=r)
  95. {
  96. int mid = (l+r)>>;
  97. if(test(mid, n))
  98. l = mid + ;
  99. else
  100. r = mid - ;
  101. }
  102. if(r<) printf("0\n");
  103. else printf("%d\n", r+);
  104. }
  105. }

POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串的更多相关文章

  1. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  2. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  3. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  4. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  5. POJ-1743 Musical Theme(后缀数组)

    题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...

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

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

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

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

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

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

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

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

随机推荐

  1. 2016.6.21 maven:Failure to transfer ... from ....

    问题描述: 才刚新建的工程,什么都没做,就显示pom.xml有问题,在第一行的标签上 有如下错误: 点击详情: Failure to transfer org.apache.maven:maven-p ...

  2. eclipse离线安装插件过程

    离线安装插件: 1. help -> install New Softe.. 2. 打开安装插件界面 最后点击,next, 同意事项,重启eclipse.

  3. mysql跨服务器查询

    MySQL FEDERATED引擎使用示例, 类似Oracle DBLINK 摘要: 本地MySQL数据库要访问远程MySQL数据库的表中的数据, 必须通过FEDERATED存储引擎来实现. 有点类似 ...

  4. BZOJ 4128 Matrix BSGS+矩阵求逆

    题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有修改. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一 ...

  5. quartz 应用到 spring定时任务 执行两次

    https://my.oschina.net/superkangning/blog/467487

  6. Ubuntu下安装JDK图文解析

    我们在64位的Ubuntu中安装JDK,选择的是jdk1.6.0_32版本号.安装文件名称为jdk-6u32-linux-x64.bin(这个是64位系统的),假设是32位系统的还须要去官网下载32位 ...

  7. Shell脚本之:字符串

    字符串可以用单引号,也可以用双引号,也可以不用引号. 单引号 str='this is a string' 单引号字符串的限制: 1.单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 2 ...

  8. UNP学习笔记(第三十章 客户/服务器程序设计范式)

    TCP测试用客户程序 #include "unp.h" #define MAXN 16384 /* max # bytes to request from server */ in ...

  9. SQL系列函数--字符串函数

    1.charindex函数用来寻找一个指定的字符(串)在另一个字符串中的起始位置,返回一个整数,没找到就返回0 select CHARINDEX('SQL','Microsoft SQL SERVER ...

  10. c语言中结构体指针

    1.指向结构体的指针变量: C 语言中->是一个总体,它是用于指向结构体,如果我们在程序中定义了一个结构体,然后声明一个指针变量指向这个结构体.那么我们要用指针取出结构体中的数据.就要用到指向运 ...