usaco5.1.3 Musical Themes
来源:互联网 发布:按键精灵抢红包源码 编辑:程序博客网 时间:2024/06/03 23:41
一 原题
Brian 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 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)
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)
3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 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]
二 分析
三 代码
/*ID:maxkibb3LANG:C++PROB:theme*/#include<cstdio>#include<vector>using namespace std;const int MAX = 5005;const int IMPOSSIBLE_NUM = 89;int n;int a[MAX];int b[MAX];vector<int> c;int nxt[MAX] = {-1, 0};void init() { 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]; }}void set_next(int _i) { int idx = nxt[_i - 1]; bool match = false; while(idx != -1) { if(c[_i - 1] != c[idx]) { idx = nxt[idx]; continue; } match = true; nxt[_i] = idx + 1; break; } if(!match) nxt[_i] = 0;}int kmp() { int ans = -1; for(int i = 2; i <= c.size(); i++) set_next(i); for(int i = 1; i < n;) { for(int j = 0; j < c.size();) { if(b[i] == c[j]) { i++; j++; } else if(j == 0) i++; else j = nxt[j]; if(j == c.size() - 1) ans = i - 1; if(i == n) break; } } return ans;}bool judge(int _len) { for(int i = 1; i <= n - _len; i++) { c.clear(); for(int j = 0; j < _len; j++) c.push_back(b[i + j]); c.push_back(IMPOSSIBLE_NUM); int idx = kmp(); if(idx - i < 2 * _len) continue; else return true; } return false;}void solve() { int l = 4, r = n / 2; int ans; if(!judge(4)) { printf("0\n"); return; } while(l < r) { int mid = (l + r) / 2; if(r - l == 1) mid = r; if(judge(mid)) l = mid; else r = mid - 1; } printf("%d\n", l + 1);}int main() { freopen("theme.in", "r", stdin); freopen("theme.out", "w", stdout); init(); solve(); return 0;}
USER: Qi Shen [maxkibb3]TASK: themeLANG: C++Compiling...Compile: OKExecuting... 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!
/*ID:maxkibb3LANG:C++PROB:theme*/#include<cstdio>#include<algorithm>using namespace std;const int MAX = 5005;int n, a[MAX];int b[MAX];int dp[2][MAX];int 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] = min(dp[0][j - 1] + 1, j - i - 1); if(dp[1][j] > ans) ans = dp[1][j]; } else dp[1][j] = 0; } for(int j = 0; j < n; j++) swap(dp[0][j], dp[1][j]); } if(ans < 4) printf("0\n"); else printf("%d\n", ans + 1); return 0;}
- usaco5.1.3 Musical Themes
- USACO5.1.3 Musical Themes (theme)
- USACO5.1.3 Musical Themes(theme)
- Musical Themes
- Section 5.1 Musical Themes
- USACO Section 5.1 Musical Themes
- usaco 5.1.3 Musical Themes
- USACO Musical Themes 解题报告
- [USACO 5.1.3]Musical Themes(后缀数组)
- usaco training 5.1.3 Musical Themes 题解
- USACO Section 5.1 Musical Themes - 题目转换以及KMP..
- usaco 5.1 Musical Themes(KMP或DP)
- C++——【USACO 5.1.3】——Musical Themes
- 【POJ1743】Musical Themes 乐曲主题 后缀数组、 (也可以用hash+二分做的~)
- 【USACO5.1.3】乐曲主题 动态规划
- YP.1.3 Two Recurring Themes(双语)
- style-Themes
- Xcode themes
- mt7601移植到imx287
- error: You have not concluded your merge (MERGE_HEAD exists).git拉取失败
- java-对象的克隆
- PyMC3
- Linux微信开发者工具安装
- usaco5.1.3 Musical Themes
- 二分贪心-E
- Ubuntu 下的Android studio
- 深入了解android消息队列
- 使用jpcap编写抓包程序
- DOM对象篇
- ubuntu17连接手机并且使用adb调试的准备工作
- java打包下载(新增递归压缩,指定路径压缩)
- Git 的origin和master分析