POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)

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.

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

1、长度至少为5个音符。

2、在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

3、重复出现的同一主题不能有公共部分。

思路:后缀数组。求出任意相邻音符的差值,然后把问题转化为不可重叠最长重复子串,用后缀数组来做。先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。

先不考虑重叠,重复子串的长度要大于等于k,也就是一个区间内的height值都大于等于k,当出现height小于k则重新定位。

再来考虑重叠,我们知道了一个区间的height都大于等于k,如果存在两个后缀距离大于k,那么可以肯定存在两个长度为k的子串是相同的,且不重叠。

参考代码:

  1 //#include<bits/stdc++.h>
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<cstdlib>
  7 #include<string>
  8 using namespace std;
  9 #define clr(a,val) memset(a,val,sizeof a)
 10 const int maxm=20010;
 11 int N;
 12 struct SuffixArray{
 13     int s[maxm];
 14     int sa[maxm],height[maxm],rank[maxm],n;
 15     int t[maxm*2],t2[maxm*2];
 16     long long cnt[maxm];
 17     void build_sa(int m){
 18         int i,*x=t,*y=t2;
 19         for(i=0;i<m;i++) cnt[i]=0;
 20         for(i=0;i<n;i++) cnt[x[i]=s[i]]++;
 21         for(i=1;i<m;i++) cnt[i]+=cnt[i-1];
 22         for(i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i;
 23         for(int k=1,p=0;k<n;k <<=1)
 24         {
 25             p=0;
 26             for(i=n-k;i<n;i++) y[p++]=i;
 27             for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
 28             for(i=0;i<m;i++) cnt[i]=0;
 29             for(i=0;i<n;i++) cnt[x[y[i]]]++;
 30             for(i=1;i<m;i++) cnt[i]+=cnt[i-1];
 31             for(i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i];
 32             swap(x,y);
 33             p=1;x[sa[0]]=0;
 34             for(i=1;i<n;i++)
 35                    if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
 36                     x[sa[i]]=p-1;
 37                   else x[sa[i]]=p++;
 38             if(p>=n)
 39               break;
 40             m=p;
 41         }
 42     }
 43     void build_height()
 44     {
 45         int k=0;
 46         for(int i=0;i<n;i++) rank[sa[i]]=i;
 47         for(int i=0;i<n;i++)
 48         {
 49             if(k) k--;
 50             if(!rank[i]) continue;
 51             int j=sa[rank[i]-1];
 52             while(s[i+k]==s[j+k]) k++;
 53             height[rank[i]]=k;
 54         }
 55     }
 56 } SA;
 57
 58 bool check(int key)
 59 {
 60     int tMax = SA.sa[1];
 61     int tMin = SA.sa[1];
 62     for (int i = 2; i<=N; ++i)
 63     {
 64         if (SA.height[i] < key) tMax = tMin = SA.sa[i];
 65         else
 66         {
 67             if(SA.sa[i] < tMin) tMin = SA.sa[i];
 68             if(SA.sa[i] > tMax) tMax = SA.sa[i];
 69             if(tMax - tMin > key) return true;
 70         }
 71     }
 72     return false;
 73 }
 74 int main()
 75 {
 76     while(~scanf("%d",&N)&&N)
 77     {
 78         SA.n=N;
 79         int t,k;N--;
 80         scanf("%d",&t);
 81         for(int i=0;i<N;++i)
 82         {
 83             scanf("%d",&k);
 84             SA.s[i]=k-t+100;
 85             t=k;
 86         }
 87         SA.s[N]=0;
 88         SA.build_sa(200);
 89         SA.build_height();
 90         int L=4,R=N/2,ans=0;
 91         while(L<=R)
 92         {
 93             int mid=L+R>>1;
 94             if(check(mid)) ans=mid,L=mid+1;
 95             else R=mid-1;
 96         }
 97         printf("%d\n",(ans>=4? ans+1:0));
 98     }
 99
100     return 0;
101 }

原文地址:https://www.cnblogs.com/songorz/p/10035552.html

时间: 2024-08-06 01:15:56

POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)的相关文章

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

题意:    给出一列数据,问你其中重复的最长连续子串的长度    但是有要求:        1. 长度至少为 5 .        2. 两串可以不相等,但两串每个对应位置的数字相减差值固定 (即相同变化) 分析:    因为子串变化相同,故可先把原数组前后相减, 则求出差值数组的最长重复子串的长度再 +1 就是答案.        最长重复子串的长度:        使用后缀数组.        先将问题变为判定是否存在长度为 x 的重复子串,再用二分寻找答案.        用 heig

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

题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现(或者经过调转出现,调转是主题同时加上或者减去同一个整数) 3:重复主题不能重叠 解题思路: 求调转重复出现的子串,那么主题之间的差值一定是不变的.可以求文本串s中相邻两个数的差值,重新组成一个新的文本串S,然后找S后缀串中最长公共不重叠前缀.rank相邻的后缀串,公共前缀一定最长,但是有可能重叠.

POJ 1743 Musical Theme (后缀数组)

题目大意: 刚才上88个键弹出来的音符. 如果出现重复的,或者是高一个音阶的重复的都算. 思路分析: 具体可以参考训练指南222. height数组表示按照排序后的sa最近的两个后缀的最长前缀. 将height 分块.然后二分答案,二分答案之后去判断是否满足. 要考虑到不重合,还有大于5. 所以二分的时候要从5开始,然后判断的时候要加一个 up - down >len #include <cstdio> #include <iostream> #include <alg

poj 1743 Musical Theme(男人八题&amp;后缀数组第一题)

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17298   Accepted: 5939 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

POJ 1743 Musical Theme ——后缀数组

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

后缀数组 POJ 1743 Musical Theme

题目链接 题意:给定n个数字,求超过5个数字的,最长的,变化相同的,不相交的重复子串 分析:男人8题中的一题!数列相邻两项做差,形成新数列,即求数列中的最长重复子串(不可相交). 后缀数组+二分答案.假如二分得到答案L,如何知道它是可行的呢? 因为对于排序后的后缀,Lcp ( Suffix ( List [ i ] ) , Suffix ( List [ i - 1 ] ) ) 是所有与Suffix ( List [ i ] )的LCP值中最大的一个. 因为 Height [ i ] 表示的是排

POJ - 1743 Musical Theme (后缀数组求不可重叠最长重复子串)

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 music

POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)

题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. "主题"是整个音符序列的一个子串,它需要满足如下条件: 1.长度至少为5个音符 2.在乐曲中重复出现(可能经过转调,"转调"的意思是主题序列中每个音符都被加上或减去了同一个整数值.) 3.重复出现的同一主题不能有公共部分. 思路:是要求最长不重叠重复的子串,如果没有不重叠的限制条件,那么height中的最大值即可 现在对于

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

Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 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