A musical melody is represented as a sequence of N (1 <= N <= 5000) 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 "theme", 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 longappears (potentially transposed -- see below) again somewhere else in the piece of musicis 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!
The first line of the input file contains the integer N. Each subsequent line (except potentially the last) contains 20 integers representing the sequence of notes. The last line contains the remainder of the notes, potentially fewer than 20.
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.
Your program ('theme') produced all correct answers! This is yoursubmission #5 for this problem.Congratulations!
AC代码1(动态规划): /* ID:maxkibb3 LANG:C++ PROB:theme */ #include <cstdio> #include <cstring> #include <algorithm> const int maxn = 2e4 + 10; int n, a[maxn], b[maxn], dp[2][maxn], ans; int main() { freopen("theme.in", "r", stdin); freopen("theme.out", "w", stdout); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (i == 0) continue; b[i] = a[i] - a[i - 1]; } for (int i = 1; i < n; i++) { for (int j = i + 1; j < n; j++) { if (b[j] == b[i]) { dp[1][j] = dp[0][j - 1] + 1; int tmp = std::min(dp[1][j], j - i - 1); if (tmp > ans) ans = tmp; } else dp[1][j] = 0; } memcpy(dp[0], dp[1], sizeof(dp[1])); } if (ans < 4) printf("0\n"); else printf("%d\n", ans + 1); return 0; }运行结果2: USER: Qi Shen [maxkibb3] TASK: theme LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 5584 KB] Test 2: TEST OK [0.000 secs, 5584 KB] Test 3: TEST OK [0.000 secs, 5580 KB] Test 4: TEST OK [0.000 secs, 5580 KB] Test 5: TEST OK [0.000 secs, 5584 KB] Test 6: TEST OK [0.000 secs, 5580 KB] Test 7: TEST OK [0.000 secs, 5584 KB] Test 8: TEST OK [0.000 secs, 5580 KB] Test 9: TEST OK [0.000 secs, 5584 KB] Test 10: TEST OK [0.000 secs, 5584 KB] Test 11: TEST OK [0.000 secs, 5584 KB] Test 12: TEST OK [0.000 secs, 5584 KB] Test 13: TEST OK [0.000 secs, 5580 KB] Test 14: TEST OK [0.000 secs, 5584 KB] Test 15: TEST OK [0.000 secs, 5584 KB] All tests OK.Your program ('theme') produced all correct answers! This is yoursubmission #11 for this problem. Congratulations!
AC代码2(二分答案+后缀数组): /* ID: maxkibb3 LANG: C++ PROB: theme */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using std::string; using std::sort; using std::max; using std::min; #define local const int maxn = 2e4 + 10; int n, a[maxn], b[maxn]; // All indexes start from 0 struct SuffixArray { int len, str[maxn], sa[maxn], rank[maxn], height[maxn], bucket[maxn]; struct RadixItem { int idx, key[2]; RadixItem() {} RadixItem(int a, int b, int c) { idx = a, key[0] = b, key[1] = c; } bool operator == (const RadixItem &obj) const { return key[0] == obj.key[0] && key[1] == obj.key[1]; } } items[maxn]; void RadixSort() { int x; RadixItem tmp[maxn], *r1, *r2; for (x = 1, r1 = items, r2 = tmp; x >= 0; x--) { memset(bucket, 0, sizeof(bucket)); for (int i = 0; i < len; i++) bucket[r1[i].key[x] + 1]++; for (int i = 1; i < maxn; i++) bucket[i] += bucket[i - 1]; for (int i = len - 1; i >= 0; i--) r2[--bucket[r1[i].key[x] + 1]] = r1[i]; r1 = tmp, r2 = items; } for (int i = 0, cnt = 0; i < len; i++) { rank[items[i].idx] = (i == 0 || items[i] == items[i - 1])? cnt: ++cnt; } } SuffixArray() {} SuffixArray(int *s, int l) { memcpy(str, s, sizeof(int) * l); len = l; } void CalcSA() { for (int i = 0; i < len; i++) { rank[i] = str[i] + 88; } for (int i = 1; i < 2 * len; i *= 2) { for (int j = 0; j < len; j++) { items[j] = RadixItem(j, rank[j], (j + i < len)? rank[j + i]: -1); } RadixSort(); } for (int i = 0; i < len; i++) sa[rank[i]] = i; // for (int i = 0; i < len; i++) printf("%d ", sa[i]); } void CalcHeight() { int h = 0; for (int i = 0; i < len; i++) { if (rank[i] == 0) { h = 0; } else { int x = sa[rank[i] - 1]; if (--h < 0) h = 0; for (; i + h < len && x + h < len && str[i + h] == str[x + h]; h++); } height[rank[i]] = h; } // for (int i = 0; i < len; i++) printf("%d ", height[i]); } } solver; bool judge(int x) { int minv, maxv; for (int i = 0; i < n - 1; i++) { if (i && solver.height[i] >= x) { minv = maxv = solver.sa[i - 1]; int j; for (j = i; j < n - 1 && solver.height[j] >= x; j++) { minv = min(solver.sa[j], minv); maxv = max(solver.sa[j], maxv); } if (maxv - minv > x) return true; i = --j; } } return false; } int main() { #ifdef local freopen("theme.in", "r", stdin); freopen("theme.out", "w", stdout); #endif while (scanf("%d", &n) && n) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n - 1; i++) b[i] = a[i + 1] - a[i]; solver = SuffixArray(b, n - 1); solver.CalcSA(); solver.CalcHeight(); int l = 0, r = n - 1; while (l < r) { int mid = l + ((r - l + 1) >> 1); if (judge(mid)) l = mid; else r = mid - 1; } printf("%d\n", (l > 3)? ++l: 0); break; } return 0; }