[5_1_theme] DP?

来源:互联网 发布:软件安全工程师 编辑:程序博客网 时间:2024/05/21 04:25
Musical Themes
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)
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)

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] 



















Binary search of the ans (diff the original array):
#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

Official Analysis:
Musical Themes
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);}