usaco5.1.3 Musical Themes

xiaoxiao2021-02-27  505

一 原题

Musical ThemesBrian Dean

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!

PROGRAM NAME: theme

INPUT FORMAT

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.

SAMPLE INPUT (file theme.in)

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

OUTPUT FORMAT

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 OUTPUT (file theme.out)

5 [The five-long theme is the last five notes of the first line and the first five notes of the second] 

二 分析

要求在一个长度最大为5000的串L中找到两个不相交的子串l1,l2,满足两个条件:(1)l1,l2长度相同(2)对应位置上的元素差为定值。 很容易想到把L中每个元素与前一个元素做差。我们只要求新数组中两个完全相同的子串就好了(有些处理不相交的细节注意一下)。 这道题usaco上的数据小了,可以用动态规划给一个O(n^2)的解法(见AC代码1),POJ上的数据给定字符串长度会到2e4,要用后缀数组才能过(见AC代码2)。

三 代码

运行结果1: USER: Qi Shen [maxkibb3] TASK: theme LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4256 KB] Test 2: TEST OK [0.000 secs, 4256 KB] Test 3: TEST OK [0.000 secs, 4256 KB] Test 4: TEST OK [0.000 secs, 4256 KB] Test 5: TEST OK [0.000 secs, 4256 KB] Test 6: TEST OK [0.000 secs, 4256 KB] Test 7: TEST OK [0.000 secs, 4256 KB] Test 8: TEST OK [0.000 secs, 4256 KB] Test 9: TEST OK [0.011 secs, 4256 KB] Test 10: TEST OK [0.000 secs, 4256 KB] Test 11: TEST OK [0.108 secs, 4256 KB] Test 12: TEST OK [0.076 secs, 4256 KB] Test 13: TEST OK [0.086 secs, 4256 KB] Test 14: TEST OK [0.097 secs, 4256 KB] Test 15: TEST OK [0.086 secs, 4256 KB] All tests OK.

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; }
转载请注明原文地址: https://www.6miu.com/read-2360.html

最新回复(0)