您的位置:首页 > 其它

poj 1743 Musical Theme

2017-10-11 19:49 141 查看
Musical Theme

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 31198 Accepted: 10404
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
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

题意:给你一个又旋律组成的序列,一段旋律被称为主题当满足一下条件
1:长度至少为5
2:出现至少两次且不重叠
3:可以同时变化
例如 1 2 3 4 5 和 6 7 8 9 10 。

主题序列相邻之间的差值一定相同,另每个数减去前一位的到新的序列,问题转化为求最长不想交前缀长度。二分长度k,将连续的且Height[i]》=k的分为一组,长度为同组中的位置差最大值,如果最大差值大于k则满足。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxm = 100005;
const int INF = 1e9 + 7;
int a[maxm], b[maxm], sa[maxm], s[maxm], Rank[maxm], tp[maxm], Height[maxm];
int n, m;
void Rsort()
{
for (int i = 0; i <= m; i++) s[i] = 0;
for (int i = 1; i <= n; i++) s[Rank[tp[i]]] ++;
for (int i = 1; i <= m; i++) s[i] += s[i - 1];
for (int i = n; i >= 1; i--) sa[s[Rank[tp[i]]] --] = tp[i];
}
int cmp(int *f, int x, int y, int w)
{
return f[x] == f[y] && f[x + w] == f[y + w];
}
void suffix()
{
for (int i = 1; i <= n; i++) Rank[i] = a[i], tp[i] = i;
m = 200, Rsort();
for (int w = 1, p = 1, i; p < n; w += w, m = p)
{
for (p = 0, i = n - w + 1; i <= n; i++) tp[++p] = i;
for (i = 1; i <= n; i++) if (sa[i] > w) tp[++p] = sa[i] - w;
Rsort(), swap(Rank, tp), Rank[sa[1]] = p = 1;
for (i = 2; i <= n; i++) Rank[sa[i]] = cmp(tp, sa[i], sa[i - 1], w) ? p : ++p;
}
int j, k = 0;
for (int i = 1; i <= n; Height[Rank[i++]] = k)
for (k = k ? k - 1 : k, j = sa[Rank[i] - 1]; a[i + k] == a[j + k]; k++);
}
int judge(int x)
{
int mi = INF, mx = -INF;
for (int i = 2;i <= n;i++)
{
if (Height[i] >= x)
{
mi = min(mi, min(sa[i], sa[i - 1]));
mx = max(mx, max(sa[i], sa[i - 1]));
if (mx - mi > x) return 1;
}
else mi = INF, mx = -INF;
}
return 0;
}
int main()
{
int i, j, k, sum, ans;
while (scanf("%d", &n), n != 0)
{
ans = 0;
for (i = 1;i <= n;i++) scanf("%d", &b[i]);
for (i = 1;i <= n;i++) a[i] = b[i] - b[i - 1] + 88;
suffix();
int l = 0, r = n * 2;
while (r - l > 1)
{
int mid = (r + l) / 2;
if (judge(mid)) l = mid, ans = mid;
else r = mid;
}
if (ans >= 4) printf("%d\n", ans + 1);
else printf("0\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: