poj 1743 Musical Theme(后缀数组)
来源:互联网 发布:mac taupe亚洲人试色 编辑:程序博客网 时间:2024/05/17 06:16
Musical Theme
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 26471 Accepted: 8936
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
解题思路:
重复2 次以上的子串的个数(不重叠)【单串】
枚举字串长度h , 对于每一次的h,利用height数组,
找出连续的height大于等于h的里面最左端和最右端得为之l和r.
如果l + h - 1 < r的话,说明没有重叠,答案加1.
注意:在找最左端l和最右端r时,假设已经找到了i和j,那么下一次i一定会从j+1开始枚举。因为i-j这一段区间都是符合height>=h的,也就是说这一段区间已经找过了,不需要重复找了。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 20005;int s[maxn],t[maxn],t2[maxn]; int Rank[maxn],height[maxn],sa[maxn]; int n,buc[maxn],a[maxn]; void getsa(int n,int m) { int i,*x = t,*y = t2; for(i = 0; i < m; i++) buc[i] = 0; for(i = 0; i < n; i++) buc[x[i] = s[i]]++; for(i = 0; i < m; i++) buc[i] += buc[i-1]; for(i = n-1; i >= 0; i--) sa[--buc[x[i]]] = i; for(int k = 1; k <= n; k = k << 1) { int p = 0; for(i = n-k; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k; for(i = 0; i < m; i++) buc[i] = 0; for(i = 0; i < n; i++) buc[x[y[i]]]++; for(i = 0; i < m; i++) buc[i] += buc[i-1]; for(i = n-1; i >= 0; i--) sa[--buc[x[y[i]]]] = y[i]; swap(x,y); p = 1, x[sa[0]] = 0; for(i = 1; i < n; i++) x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1:p++; if(p >= n) break; m = p; } } void getheight(int n) { int i,j,k = 0; for(i = 1; i <= n; i++) Rank[sa[i]] = i; for(i = 0; i < n; i++) { if(k) k--; if(Rank[i] == 0) { height[Rank[i]] = 0; continue; } j = sa[Rank[i]-1]; while(s[i+k] == s[j+k]) k++; height[Rank[i]] = k; } } bool check(int len){int cnt = 0,j = 1;for(int i = 1; i <= n; i++){j = i;int l = 0x3f3f3f3f,r = 0;while(height[j] >= len) { r = max(r,max(sa[j-1],sa[j]));l = min(l,min(sa[j-1],sa[j]));j++;}i = j; //不需要重复找区间,直接i=jif(l + len - 1 < r) return true;}return false;}int solve(){int l = 1,r = n,mid,ans;while(l <= r){mid = (l + r) >> 1;if(check(mid)){ans = mid;l = mid + 1;}else r = mid - 1;}return ans;}int main(){int Min;while(scanf("%d",&n),n){for(int i = 0; i < n; i++)scanf("%d",&a[i]);for(int i = 0; i < n - 1; i++)s[i] = a[i+1] - a[i] + 100;n--;s[n] = 0;getsa(n+1,200);getheight(n);int ans = solve() + 1;if(ans < 5) ans = 0;printf("%d\n",ans);}return 0;}
0 0
- poj 1743 Musical Theme 后缀数组
- POJ 1743 Musical Theme 后缀数组
- poj 1743 Musical Theme 【后缀数组】
- poj 1743 Musical Theme 后缀数组
- POJ 1743 Musical Theme(后缀数组)
- POJ 1743 Musical Theme(后缀数组)
- poj 1743 Musical Theme 后缀数组
- poj 1743 Musical Theme(后缀数组)
- POJ 1743 Musical Theme (后缀数组)
- 【后缀数组】 POJ 1743 Musical Theme
- 【POJ 1743】Musical Theme【后缀数组】
- POJ 1743 Musical Theme 后缀数组
- [后缀数组+二分] poj 1743 Musical Theme
- 【后缀数组】【poj 1743】Musical Theme
- 【POJ】1743 Musical Theme 【后缀数组】
- POJ 1743 Musical Theme(后缀数组)
- [POJ 1743]Musical Theme(后缀数组)
- POJ 1743 Musical Theme(后缀数组)
- mysql可视化工具官方教程
- NSCTF crypto100
- Boa服务器移植及应用(2)
- MagicIndicator系列之一 —— 使用MagicIndicator打造千变万化的ViewPager指示器
- 网络编程基础
- poj 1743 Musical Theme(后缀数组)
- MagicIndicator系列之二 —— MagicIndicator使用指南
- Python控制Ubuntu1404关机、重启
- HDU2030汉字统计
- 关于Android app 国际化 中英文翻译的细节处理
- GlobalAlloc 分配的内存上限
- MagicIndicator系列之三 —— MagicIndicator原理浅析及扩展MagicIndicator的4种方式
- adb 查看最上层成activity名字
- 单元测试:Mockito