[5_1_theme] DP?
来源:互联网 发布:软件安全工程师 编辑:程序博客网 时间:2024/05/21 04:25
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]
#include <fstream>using namespace std;bool check(int n, int b[], int t) { int m = n - t * 2 - 1; int o = n - t; for (int i = 0; i <= m; ++i) { for (int j = i + t + 1; j <= o; ++j) { int k; for (k = 0; k < t; ++k) if (b[i + k] != b[j + k]) break; if (k >= t) return true; } } return false;}int main() { ifstream fin("theme.in"); int n, a[5000], b[5000]; fin >> n; for (int i = 0; i < n; ++i) fin >> a[i]; for (int i = 0; i < n - 1; ++i) b[i] = a[i + 1] - a[i]; fin.close(); int low = 0, high = n, mid; while (low < high) { mid = (low + high + 1) >> 1; if (check(n - 1, b, mid)) low = mid; else high = mid - 1; } ofstream fout("theme.out"); fout << (low < 4 ? 0 : low + 1) << endl; fout.close(); return 0;}
Many algorithms with different complexities: http://www.nocow.cn/index.php/USACO/theme
Alex Schwendner
Let theme(i,j) be the length of the longest theme which occurs starting at both note i and j. Note that if note[i+1]-note[i] == note[j+1]-note[j], than theme(i,j) = 1+theme(i+1,j+1). Otherwise, theme(i,j) = 1. Thus, we order the search in such a way that theme(i,j) is tested immediately after theme(i+1,j+1), keeping track of the length of the current theme, as well as the length of the longest theme found so far.
#include <fstream.h>int n;int note[5000];int main () { ifstream filein ("theme.in"); filein >> n; for (int i = 0; i < n; ++i) filein >> note[i]; filein.close (); int longest = 1; for (int i = 1; i < n; ++i) {int length = 1;for (int j = n - i - 1 - 1; j >= 0; --j) { if (note[j] - note[j + 1] == note[j + i] - note[j + i + 1]) {++length;if (length > i) length = i;if (longest < length) longest = length; } else {length = 1; }} } ofstream fileout ("theme.out"); fileout << ((longest >= 5) ? longest : 0) << endl; fileout.close (); exit (0);}
- [5_1_theme] DP?
- DP(5)
- DP经典5题 【DP】
- DP·数位DP(5)
- DP经典5题
- DP经典5题
- dp
- dp
- dp
- 【DP】
- dp
- dp
- DP
- DP
- DP
- DP
- DP
- dp
- 《大话设计模式》之--第3章 拍摄UFO----单一职责原则
- 《大话设计模式》之--第4章 考研求职两不误----开放-封闭原则
- 《大话设计模式》之--第5章 会修电脑不会修收音机?----依赖倒转原则
- Cocos2d-x之testCpp---Accelerometer(重力感应)分析
- 《大话设计模式》之--第6章 穿什么有这么重要?----装饰模式
- [5_1_theme] DP?
- Linux进程间通信(IPC)
- 计算最大公因数的欧几里德算法
- 《大话设计模式》之--第7章 为别人做嫁衣----代理模式
- LISP 简单的数据库 3.3 查看数据库内容
- python speech recognition 声音识别
- Android文件管理器与media数据库的同步问题
- 《大话设计模式》之--第8章 雷锋依然在人间----工厂方法模式
- MFC:Tab Control 控件的使用