Musical Theme
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26260 | Accepted: 8865 |
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
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.
题意:对给出的n个数用后一个数减去前一个数做差,形成一个新的串。然后问这个串的不可重叠最长重复子串是多少?若答案x小于4,则输出0,否则输出x+1。
本来以为是裸题,就那么随意的交了一发,只简单计算每个height[i]的两个sa值,嗯,妥妥wa。。。
后来知道是09年论文上的题目,研究了一下给的思路,要二分去找答案(感觉那个k就是答案啊,那么多人题解都写的那么玄乎,欺负我等小盆友看不懂)
不过这个二分确实不好写,很难写过去。原先卿爷说我的二分写的太挫,强行让我换了种写法(其实卿爷确实有道理,原先的方法容易坑在一些莫名其妙的地方)这次发现还是我原来的写法能过23333虽然我觉得卿爷知道了一定会说我写挫了。。。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int M = 2e4 + 10;
int w[M];
int rankk[M * 2];
int sa[M];
int height[M];
int s[M];
void get_sa_hei(int len)
{
static int m[M], tmp[M];
//int len = strlen(s + 1);
memset(rankk, 0, sizeof(int) * len * 2);
for (int i = 0; i <= 255; i++) w[i] = 0;
for (int i = 1; i <= len; i++) w[s[i]]++;
for (int i = 1; i <= 255; i++) w[i] += w[i - 1];
for (int i = len; i; i--) tmp[w[s[i]]--] = i;
rankk[tmp[1]] = 1;
for (int i = 2; i <= len; i++)
rankk[tmp[i]] = rankk[tmp[i-1]] + (s[tmp[i]] != s[tmp[i-1]]);
//get sa 用基数排序
for (int k = 1; k <= len; k <<= 1) {
for (int i = 0; i <= len; i++) w[i] = 0;
for (int i = 1; i <= len; i++) w[rankk[i+k]]++;
for (int i = 1; i <= len; i++) w[i] += w[i-1];
for (int i = len; i; i--) m[w[rankk[i+k]]--] = i;
for (int i = 0; i <= len; i++) w[i] = 0;
for (int i = 1; i <= len; i++) w[rankk[i]]++;
for (int i = 1; i <= len; i++) w[i] += w[i-1];
for (int i = len; i; i--) tmp[w[rankk[m[i]]]--] = m[i];
m[tmp[1]] = 1;
for (int i = 2; i <= len; i++)
m[tmp[i]] = m[tmp[i-1]] + (rankk[tmp[i]] != rankk[tmp[i-1]] || rankk[tmp[i]+k] != rankk[tmp[i-1]+k]);
for (int i = 1; i <= len; i++) rankk[i] = m[i];
}
for (int i = 1; i <= len; i++) sa[rankk[i]] = i;
//get height
for (int i = 1, j = 0; i <= len; i++) {
if (rankk[i] == 1) continue;
for (j ? j-- : 0; s[sa[rankk[i]-1]+j] == s[i+j]; j++);
height[rankk[i]] = j;
}
}
bool check(int n, int x) //检验一下此时的答案能不能行,二分答案时候的基本套路
{
int mmax = sa[1], mmin = sa[1];
for (int i = 2; i < n; i++) {
if (height[i] >= x) {
mmax = max(mmax, sa[i]);
mmin = min(mmin, sa[i]);
if (mmax - mmin > x) return true;
}
else mmax = mmin = sa[i];
}
return false;
}
int main()
{
int n;
while (scanf("%d", &n) && n) {
int len = 0;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
if (i == 0) {
y = x;
continue;
}
s[++len] = x - y + 88;
y = x;
}
get_sa_hei(len);
int ans = -1;
int l = 1, r = n;
while (l <= r) { //这个二分= = 卿爷讲课糊我确实也觉得这种二分很容易出事啊。。
int mid = (l + r) / 2;
if (check(n, mid)) {
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
//cout << "~~" << ans <<endl;
if (ans < 4) ans = -1;
printf("%d\n", ans + 1);
}
return 0;
}