Musical Theme
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 35011 Accepted: 11612 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
Hint
Use scanf instead of cin to reduce the read time.
Source
【思路】
先恭喜我自己终于成为楼教主指定的八分之三的真男人了。
因为每段音符的key是可以升降的,寻找音乐主题段出现与否,重点关注差值序列是否一致,所以先对数组做差,于是问题转化成了求最长的不重合的重复子序列的长度。对差值数组构造后缀数组,再造height数组,二分答案,每次看在height里扫一遍能否找到第一个字符位置相距距离大于k的两个子串。总复杂度
【代码】
//******************************************************************************
// File Name: POJ_1743.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: Thu 18 Oct 2018 04:16:22 PM CST
//******************************************************************************
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 20005;
int n;
int s[MAXN], sa[MAXN], rk[MAXN], height[MAXN], c[MAXN], x[MAXN], y[MAXN];
void get_sa()
{
int m = 255;
for (int i = 1; i <= m; i++) c[i] = 0;
for (int i = 1; i <= n; i++) c[x[i] = s[i]]++;
for (int i = 2; i <= m; i++) c[i] += c[i - 1];
for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
for (int k = 1; k <= n; k <<= 1) {
int num = 0;
for (int i = n - k + 1; i <= n; i++) y[++num] = i;
for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i] - k;
for (int i = 1; i <= m; i++) c[i] = 0;
for (int i = 1; i <= n; i++) c[x[i]]++;
for (int i = 2; i <= m; i++) c[i] += c[i - 1];
for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
swap(x, y);
x[sa[1]] = num = 1;
for (int i = 2; i <= n; i++)
x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
if (num == n) break;
m = num;
}
}
void get_height()
{
for (int i = 1; i <= n; i++) rk[sa[i]] = i;
int k = 0;
for (int i = 1; i <= n; i++) {
if (rk[i] == 1) continue;
if (k) k--;
int j = sa[rk[i] - 1];
while (j + k <= n && i + k <= n && s[i + k] == s[j + k]) k++;
height[rk[i]] = k;
}
}
bool check(int xx)
{
int mn = sa[1], mx = sa[1];
for (int i = 2; i <= n; i++) {
if (height[i] < xx)
mn = mx = sa[i];
else {
if (sa[i] < mn) mn = sa[i];
if (sa[i] > mx) mx = sa[i];
if (mx - mn > xx) return true;
}
}
return false;
}
int main()
{
// freopen("in.txt", "r", stdin);
while (scanf("%d", &n) == 1 && n) {
for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
for (int i = n; i >= 1; i--) s[i] = s[i] - s[i - 1] + 88;
get_sa();
get_height();
int l = 0, r = n >> 1, ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
if (ans + 1 >= 5)
printf("%d\n", ans + 1);
else
printf("0\n");
}
return 0;
}