Language:
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! 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.
Source |
#include <cstdio>
#include <cstring>
const int N = 20010;
int wa[N], wb[N], wv[N], ws[N];
int ary[N], sa[N];
bool 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 = wa, *y = wb, *t;
for (i = 0; i < m; ++i) ws[i] = 0;
for (i = 0; i < n; ++i) ws[x[i] = ary[i]]++;
for (i = 1; i < m; ++i) ws[i] += ws[i - 1];
for (i = n - 1; i >= 0; --i) sa[--ws[x[i]]] = i;
for (j = p = 1; p < n; m = p, j *= 2) {
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) ws[i] = 0;
for (i = 0; i < n; ++i) ws[wv[i]]++;
for (i = 1; i < m; ++i) ws[i] += ws[i -1];
for (i = n - 1; i >= 0; --i) sa[--ws[wv[i]]] = y[i];
t = x, x = y, y = t;
for (x[sa[0]] = 0, p = i = 1; i < n; ++i)
x[sa[i]] = Cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
}
int height[N], rank[N];
void calheight(int n) {
int i, j, k = 0;
for (i = 1; i <= n; ++i) rank[sa[i]] = i;
for (i = 0; i < n; height[rank[i++]] = k)
for (k ? --k : 0, j = sa[rank[i] - 1]; ary[i + k] == ary[j + k]; ++k);
}
bool check(int k, int n) {
int min_val = sa[1], max_val = sa[1];
for (int i = 2; i < n; ++i) {
if (height[i] < k)
min_val = max_val = sa[i];
else {
if (sa[i] < min_val) min_val = sa[i];
if (sa[i] > max_val) max_val = sa[i];
if (max_val - min_val >= k) return true;
}
}
return false;
}
int solve(int l, int r, int n) {
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid, n))
l = mid + 1;
else
r = mid - 1;
}
return l;
}
int main() {
int n;
while (~scanf("%d", &n), n) {
for (int i = 0; i < n; ++i)
scanf("%d", ary + i);
if (n < 10) {
puts("0");
continue;
}
for (int i = 0; i < n - 1; ++i)
ary[i] = ary[i + 1] - ary[i] + 100;
DA(n + 1, 300);
calheight(n);
int ans = solve(0, n / 2, n);
if (ans < 5)
puts("0");
else
printf("%d\n", ans);
}
return 0;
}