首页 > 代码库 > POJ 1743. Musical Theme
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 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:
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!
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.
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
3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800
Sample Output
5
Hint
Use scanf instead of cin to reduce the read time.
题意大概就是取差值求一遍不重复的字串,唯一就是至少长度为5,而且重复字串不能连续(因为连续了说明原数组重叠了)。
#include <iostream>#include <cstring>#include <cstdio>const int N = 20000 + 7;using namespace std;int n,r[N],wb[N],wss[N],wv[N],sa[N],rank[N],li[N];int cmp(int *r,int a,int b,int l){ return r[a] == r[b] && r[a+l] == r[b+l];}void Da(int n,int m){ int i,j,p,*x = rank, *y = wb, *t; for(i = 0; i < m; ++i) wss[i] = 0; for(i = 0; i < n; ++i) ++wss[x[i]=r[i]]; for(i = 1; i < m; ++i) wss[i] += wss[i-1]; for(i = n-1; i >= 0; --i ) sa[--wss[x[i]]] = i; for(j = p = 1; p < n; j <<= 1, m = p) { for(p = 0,i = n-j; i < n; ++i) y[p++] = i; for(i = 0; i < n; ++i) if(sa[i] >= j) y[p++] = sa[i] - j; for(i = 0; i < n; ++i) wv[i] = x[y[i]]; for(i = 0; i < m; ++i) wss[i] = 0; for(i = 0; i < n; ++i) ++wss[wv[i]]; for(i = 1; i < m; ++i) wss[i] += wss[i-1]; for(i = n-1; i >= 0; --i) sa[--wss[wv[i]]] = y[i]; for(t = x, x = y, y = t, p = i = 1, x[sa[0]] = 0; i < n; ++i) x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++; }}int f[N], hi[N];void cal_height( ){ int i,j,k = 0; for(i = 1; i <= n; ++i) f[sa[i]] = i; for(i = 0; i < n; hi[f[i++]] = k) for(k?k--:0,j = sa[f[i]-1]; r[i+k] == r[j+k]; ++k);}bool check(int len){ int minx = n + 1 , maxx = -1,i; for(i = 2; i <= n; ++i) { if(hi[i] >= len) { minx = min(minx,min(sa[i],sa[i-1])); maxx = max(maxx,max(sa[i],sa[i-1])); } else { if(minx + len < maxx) return true; minx = n + 1, maxx = -1; } } if(minx + len < maxx) return true; return false;}int binary_search( ){ int L = 1, R = ( n >> 1 )+ 1, mid, ret = -1; while(L <= R) { mid = ( L + R ) >> 1; if(check(mid)) L = mid + 1, ret = max(ret , mid); else R = mid - 1; } return ret;}int main(){ while(~scanf("%d",&n) && n) { int i; memset(r,0,sizeof(r)); for(i = 0; i < n; ++i) scanf("%d",r+i); for(i = 0; i < n; ++i) r[i] = r[i+1] - r[i] + 100; ; n--; r[n] = 0; Da(n+1,201); cal_height( ); int ans = binary_search()+1; if( ans < 5) puts("0"); else printf("%d\n",ans); } return 0;}
POJ 1743. Musical Theme
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。