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

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


不重叠最长重复子串
冷静的说一下做法:
1.差分之后体现的是变化率,解决可以+-问题(就像是波形一样)  【注意原序列相差满足x 差分序列相差满足x+1 因为差分后每个元素代表两个元素,得到的结果长度至少是2 如1 2 3-->1 1不行】
2.不重叠的重复子串,也是二分最长长度然后分组,同一组内height满足了最长长度的要求一组内sa值相差问题(因为sa[i]表示了位置),mx-mn>=k的话可以
 
我靠什么鬼.........................该死该死该死该死什么问题啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
难道是后缀数组的问题?不能啊
该死(曾经这里有几十行这个词...)
 
【2016-12-28 00:15:57】
发现问题了,傻逼的对于n=1特判结果后面的一个数没读入..........
实践证明我的模板是对的
 

【2017-02-06】

除了分组还有一种做法,从大到小枚举L然后合并 维护并查集内mx和mn看看是不是>L(注意并查集用的编号是排序后的排名)

为什么>L 因为 1 2 3-->1 1 两个其实是重合的

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. const int N=2e4+;
  6. inline int read(){
  7. char c=getchar();int x=,f=;
  8. while(c<''||c>''){if(c=='-')f=-; c=getchar();}
  9. while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
  10. return x*f;
  11. }
  12. int n,m,k;
  13. int s[N];
  14. int sa[N],c[N],t1[N],t2[N],height[N],rnk[N];
  15.  
  16. void getHeight(){
  17. int k=;
  18. for(int i=;i<=n;i++) rnk[sa[i]]=i;
  19. for(int i=;i<=n;i++){
  20. if(k) k--;
  21. if(rnk[i]==) continue;
  22. int j=sa[rnk[i]-];
  23. while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
  24. height[rnk[i]]=k;
  25. }
  26. }
  27. inline bool cmp(int *r,int a,int b,int j){
  28. return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
  29. }
  30. void getSA(){
  31. m=;
  32. int *r=t1,*k=t2;
  33. for(int i=;i<=m;i++) c[i]=;
  34. for(int i=;i<=n;i++) c[r[i]=s[i]]++;
  35. for(int i=;i<=m;i++) c[i]+=c[i-];
  36. for(int i=n;i>=;i--) sa[c[r[i]]--]=i;
  37.  
  38. for(int j=;j<=n;j<<=){
  39. int p=;
  40. for(int i=n-j+;i<=n;i++) k[++p]=i;
  41. for(int i=;i<=n;i++) if(sa[i]>j) k[++p]=sa[i]-j;
  42.  
  43. for(int i=;i<=m;i++) c[i]=;
  44. for(int i=;i<=n;i++) c[r[k[i]]]++;
  45. for(int i=;i<=m;i++) c[i]+=c[i-];
  46. for(int i=n;i>=;i--) sa[c[r[k[i]]]--]=k[i];
  47.  
  48. swap(r,k);p=;r[sa[]]=++p;
  49. for(int i=;i<=n;i++) r[sa[i]]=cmp(k,sa[i],sa[i-],j)?p:++p;
  50. if(p>=n) break;m=p;
  51. }
  52. }
  53.  
  54. struct edge{
  55. int v,ne;
  56. }e[N];
  57. int h[N],cnt;
  58. inline void ins(int u,int v){
  59. cnt++;
  60. e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
  61. }
  62. int fa[N],mn[N],mx[N];
  63. int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
  64. void unn(int x,int y){
  65. x=find(x);y=find(y);
  66. if(x!=y){
  67. fa[x]=y;
  68. mn[y]=min(mn[y],mn[x]);
  69. mx[y]=max(mx[y],mx[x]);
  70. }
  71. }
  72. void solve(){
  73. cnt=;memset(h,,sizeof(h));
  74. for(int i=;i<=n;i++){
  75. fa[i]=i,mn[i]=mx[i]=sa[i];
  76. ins(height[i],i);
  77. }
  78. for(int L=n-;L>=;L--){
  79. for(int i=h[L];i;i=e[i].ne) unn(e[i].v,e[i].v-);
  80. if(h[L]){
  81. int x=find(e[h[L]].v);
  82. if(mx[x]-mn[x]>L) {printf("%d\n",L+);return;}
  83. }
  84. }
  85. puts("");
  86. }
  87. int main(){
  88. freopen("in","r",stdin);
  89. while((n=read())){
  90. n--;
  91. int last=read(),x;
  92. for(int i=;i<=n;i++){
  93. x=read();
  94. s[i]=x-last+;
  95. last=x;
  96. }
  97. getSA();
  98. getHeight();
  99. solve();
  100. }
  101. }

SA+并查集

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. const int N=2e4+;
  6. inline int read(){
  7. char c=getchar();int x=,f=;
  8. while(c<''||c>''){if(c=='-')f=-; c=getchar();}
  9. while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
  10. return x*f;
  11. }
  12. int n,m,k;
  13. int s[N];
  14. int sa[N],c[N],t1[N],t2[N];
  15. inline bool cmp(int *r,int a,int b,int j){
  16. return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
  17. }
  18. int rnk[N],height[N];
  19. void getHeight(){
  20. int k=;
  21. for(int i=;i<=n;i++) rnk[sa[i]]=i;
  22. for(int i=;i<=n;i++){
  23. if(k) k--;
  24. if(rnk[i]==) continue;
  25. int j=sa[rnk[i]-];
  26. while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
  27. height[rnk[i]]=k;
  28. }
  29. }
  30. void buildSA(){
  31. int *r=t1,*y=t2;
  32. for(int i=;i<=m;i++) c[i]=;
  33. for(int i=;i<=n;i++) c[r[i]=s[i]]++;
  34. for(int i=;i<=m;i++) c[i]+=c[i-];
  35. for(int i=n;i>=;i--) sa[c[r[i]]--]=i;
  36.  
  37. for(int j=;j<=n;j<<=){
  38. int p=;
  39. for(int i=n-j+;i<=n;i++) y[++p]=i;
  40. for(int i=;i<=n;i++) if(sa[i]>j) y[++p]=sa[i]-j;
  41.  
  42. for(int i=;i<=m;i++) c[i]=;
  43. for(int i=;i<=n;i++) c[r[y[i]]]++;
  44. for(int i=;i<=m;i++) c[i]+=c[i-];
  45. for(int i=n;i>=;i--) sa[c[r[y[i]]]--]=y[i];
  46.  
  47. swap(r,y);p=;r[sa[]]=++p;
  48. for(int i=;i<=n;i++) r[sa[i]]=cmp(y,sa[i],sa[i-],j)?p:++p;
  49. if(p>=n) break;m=p;
  50. }
  51. getHeight();
  52. }
  53.  
  54. bool check(int mid){
  55. int mn=sa[],mx=sa[];
  56. for(int i=;i<=n;i++){
  57. if(height[i]>=mid){
  58. mn=min(mn,sa[i]);
  59. mx=max(mx,sa[i]);
  60. if(mx-mn>mid) return true;
  61. }else mn=mx=sa[i];
  62. }
  63. return false;
  64. }
  65. void solve(){
  66. int l=,r=n>>,ans=;
  67. while(l<=r){
  68. int mid=(l+r)>>;
  69. if(check(mid)) ans=mid,l=mid+;
  70. else r=mid-;
  71. }
  72. if(ans+<) puts("");
  73. else printf("%d\n",ans+);
  74. }
  75. int main(){
  76. //freopen("in.txt","r",stdin);
  77. while((n=read())){
  78. n--;m=;
  79. int last=read(),x;
  80. for(int i=;i<=n;i++){
  81. x=read();
  82. s[i]=x-last+;
  83. last=x;
  84. }
  85. buildSA();
  86. solve();
  87. }
  88. }
 
 

POJ1743 Musical Theme [后缀数组]的更多相关文章

  1. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  2. 线性判别分析LDA原理总结

    在主成分分析(PCA)原理总结中,我们对降维算法PCA做了总结.这里我们就对另外一种经典的降维方法线性判别分析(Linear Discriminant Analysis, 以下简称LDA)做一个总结. ...

  3. ArcGIS 10.0紧凑型切片读写方法

    首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...

  4. 为什么 NaN 不等于自身?

    NaN 即Not a Number , 不是一个数字, 那么NaN到底是什么呢? 话说在JavaScript中,有6大数据类型,分别包括string,number,boolean,undefined, ...

  5. Ubuntu 16.10 安装byzanz截取动态效果图工具

    1.了解byzanz截取动态效果图工具 byzanz能制作文件小,清晰的GIF动态效果图,不足就是,目前只能通过输入命令方式来录制. byzanz主要的参数选项有: -d, --duration=SE ...

  6. 浅谈web攻防

    CSRF 跨站请求伪造(Cross-Site Request Forgery) -原理- 从上图可以看出,要完成一次CSRF攻击,受害者必须依次完成两个步骤: 1.登录受信任网站A,并在本地生成Coo ...

  7. vs2010静态链接MFC库报链接错误

    由于需要将MFC程序在其它电脑上运行,所以需要将动态链接的MFC改成静态链接,本以为很简单,没想到链接的时候出现下面的链接错误: uafxcw.lib(afxmem.obj) : error LNK2 ...

  8. IE8/9 本地预览上传图片

    本地预览的意思是,在选择图片之后先不上传到服务器,而是由一个<img>标签来预览本地的图片,非 IE8/9 浏览器可以从<input type="file"/&g ...

  9. 接口--interface

    “interface”(接口)关键字使抽象的概念更深入了一层.我们可将其想象为一个“纯”抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不规定方法主体.接口也包含了基本数据 ...

  10. Android 在Android代码中执行命令行

    1.路径最好不要是自己拼写的路径/mnt/shell/emulated/0/wifidog.conf 最好是通过方法获取的路径,不然可能导致命令无效  (挂载点的原因) public static f ...