描述
传送门:我是传送门
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!
输入
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.
输出
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.
样例
输入
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
输出
5
题意
有N(1<=N<=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的子串,它需要满足如下条件:1.长度至少为5个音符。 2.在乐曲中重复出现(就是出现过至少两次)。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值) 3.重复出现的同一主题不能有公共部分。
如果存在长度至少为5个音符的子串,则输出最长的子串的长度,否则输出0
思路
首先贴一下罗穗骞大佬的论文里的做法
先二分答案, 把题目变成判定性问题: 判断是否存在两个长度为 k 的子串是相同的,且不重叠。解决这个问题的关键还是利用 height 数组。把排序后的后缀分成若干组,其中每组的后缀之间的height 值都 不小于 k。例如,字符串为 “aabaaaab”,当 k=2 时,后缀分成了 4 组,如图 5 所示。
容易看出, 有希望成为最长公共前缀不小于k 的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的 sa 值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。整个做法的时间复杂度为O(nlogn)。本题中利用 height 值对后缀进行分组的方法很常用,请读者认真体会。
说实话我当时根本没看懂大佬所表达的意思,尤其是这个分组,我现在也没看懂why。我是看了别人的博客以后又错了无数发才写出来的···
记录一下我对这个题的理解
首先要清楚sa[]所表达的意思 “排第几的是谁?”
看图:
height[]是在sa[]的基础上所求出来的
height[i]表示sa[i]与sa[i-1]所对应的字符串的最长公共前缀
(这也是在代码中每次进行判断不重叠的原理)
1 |
mm=min(mm,min(sa[i],sa[i-1])); |
最难的部分说完了,剩下的就是对输入的处理了,由于有这么一个要求(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值),因此要找到一个方法巧妙的处理这个问题。
举个栗子:1 2 3 4 —> 7 8 9 10(1 2 3 4 经过转调以后,变为7 8 9 10)。但是不难发现,无论它如何转调,他们的相对差值是不会变的,因此我们处理一下将数据改为它与前一位数的差值,便解决了。
代码
1 /* 2 * ============================================= 3 * 4 * Filename: A.cpp 5 * 6 * Link: http://poj.org/problem?id=1743 7 * 8 * Version: 1.0 9 * Created: 2018/10/01 15时23分10秒 10 * Revision: none 11 * Compiler: g++ 12 * 13 * Author: 杜宁元 (https://duny31030.top/), duny31030@126.com 14 * Organization: QLU_浪在ACM 15 * 16 * ============================================= 17 */ 18 #include<cstdio> 19 #include<cstring> 20 #include<algorithm> 21 using namespace std; 22 #define clr(a, x) memset(a, x, sizeof(a)) 23 #define rep(i,a,n) for(int i=a;i<=n;i++) 24 #define pre(i,a,n) for(int i=n;i>=a;i--) 25 #define ll long long 26 #define max3(a,b,c) fmax(a,fmax(b,c)) 27 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 28 const double eps = 1e-6; 29 const int INF = 0x3f3f3f3f; 30 const int mod = 1e9 + 7; 31 const int MAXN = 2e6+10;; 32 33 int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN]; 34 int cmp(int *r,int a,int b,int l) 35 { 36 return r[a]==r[b] && r[a+l]==r[b+l]; 37 } 38 int sa[MAXN],rk[MAXN],height[MAXN]; 39 void SA(int *r,int n,int m) 40 { 41 int *x=wa,*y=wb; 42 43 for(int i=0; i<m; ++i) ws[i]=0; 44 for(int i=0; i<n; ++i) ++ws[x[i]=r[i]]; 45 for(int i=1; i<m; ++i) ws[i]+=ws[i-1]; 46 for(int i=n-1; i>=0; --i) sa[--ws[x[i]]]=i; 47 48 int p=1; 49 for(int j=1; p<n; j<<=1,m=p) 50 { 51 p=0; 52 for(int i=n-j; i<n; ++i) y[p++]=i; 53 for(int i=0; i<n; ++i) if(sa[i]>=j) y[p++]=sa[i]-j; 54 for(int i=0; i<n; ++i) wv[i]=x[y[i]]; 55 for(int i=0; i<m; ++i) ws[i]=0; 56 for(int i=0; i<n; ++i) ++ws[wv[i]]; 57 for(int i=1; i<m; ++i) ws[i]+=ws[i-1]; 58 for(int i=n-1; i>=0; --i) sa[--ws[wv[i]]]=y[i]; 59 swap(x,y); x[sa[0]]=0; p=1; 60 for(int i=1; i<n; ++i) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; 61 } 62 63 for(int i=1; i<n; ++i) rk[sa[i]]=i; 64 int k=0; 65 for(int i=0; i<n-1; height[rk[i++]]=k) 66 { 67 if(k) --k; 68 for(int j=sa[rk[i]-1]; r[i+k]==r[j+k]; ++k); 69 } 70 } 71 72 int n,a[MAXN],r[MAXN]; 73 74 bool ok(int k) 75 { 76 int mx = -INF,mn = INF; 77 // height[1] 没意义 i从2开始 78 for(int i = 2;i <= n;i++) 79 { 80 if(height[i] >= k) 81 { 82 mn = min(mn,min(sa[i],sa[i-1])); 83 mx = max(mx,max(sa[i],sa[i-1])); 84 if(mx-mn>k) 85 return true; 86 } 87 else 88 mx = -INF,mn = INF; 89 } 90 return false; 91 } 92 93 94 int main() 95 { 96 #ifdef ONLINE_JUDGE 97 #else 98 freopen("in.txt","r",stdin); 99 // freopen("out.txt","w",stdout); 100 #endif 101 while(~scanf("%d",&n) && n) 102 { 103 for(int i = 0;i < n;i++) 104 scanf("%d",&a[i]); 105 for(int i = 0;i < n-1;i++) 106 r[i] = a[i+1]-a[i]+88; 107 r[n-1] = 0; 108 SA(r,n,176); 109 int l = 0,r = n; 110 while(l < r) 111 { 112 int mid = (l+r+1)>>1; 113 if(ok(mid)) 114 l = mid; 115 else 116 r = mid-1; 117 } 118 if(l >= 4) 119 printf("%d\n",l+1); 120 else 121 printf("0\n"); 122 } 123 fclose(stdin); 124 // fclose(stdout); 125 return 0; 126 }