1.长度至少为5个音符。
2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)3.重复出现的同一主题不能有公共部分。
首先看到转调,这很重要,一个序列中的数加上同一个数变成另一个序列,这两个序列就是同一个主题,也就是两数列分别对相邻的数做差,应该是一一对应相等的。
转化完之后就发现是一个不重叠最长重复子串问题了,况且给的数都这么小,明摆着让你基数排序用的。
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cstdlib> #define MAXN 22222 using namespace std; int r[MAXN]; int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN]; int sa[MAXN]; //index range 1~n value range 0~n-1 int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } void da(int *r, int *sa, int n, int m) { int i, j, p, *x = wa, *y = wb, *ws = tmp; for (i = 0; i < m; i++) ws[i] = 0; for (i = 0; i < n; i++) ws[x[i] = r[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 = 1, p = 1; p < n; j *= 2, m = p) { 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]; for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } } int rank[MAXN]; //index range 0~n-1 value range 1~n int height[MAXN]; //index from 1 (height[1] = 0) void calheight(int *r, int *sa, 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]; r[i + k] == r[j + k]; ++k); return; } int a[MAXN]; bool check(int mid, int n) { int flag = 0; int mx = -1, mi = n; for(int i = 2; i <= n + 1; i++) { if((i == n + 1 && flag) || (height[i] < mid && flag)) { flag = 0; mx = max(mx, sa[i - 1]); mi = min(mi, sa[i - 1]); if(mx - mi > mid) return true; mi = n, mx = -1; } else if(height[i] >= mid) { flag = 1; mx = max(mx, sa[i - 1]); mi = min(mi, sa[i - 1]); } } return false; } int main() { int n; while(scanf("%d", &n) != EOF && n) { for(int i = 0; i < n; i++) scanf("%d", &a[i]); for(int i = 0; i < n - 1; i++) r[i] = a[i + 1] - a[i] + 89; r[--n] = 0; da(r, sa, n + 1, 200); calheight(r, sa, n); int low = 4, high = n / 2, ans = 0; while(low <= high) { int mid = (low + high) >> 1; if(check(mid, n)) { low = mid + 1; ans = max(ans, mid); } else high = mid - 1; } if(ans < 4) printf("0\n"); else printf("%d\n", ans + 1); } return 0; }