zoukankan      html  css  js  c++  java
  • 【USACO 5.1.3】Musical Themes

    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)

    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]


    ANALYSIS


    At first,我们可以知道"Transposed"是可以用相邻两数之差表示。

    FOR EXAMPLE:

      notes:1,2,3,4,5,6,7,8,9,10

      change into:1,1,1,1,1,1,1,1,1,1,-10 (PAY ATTENTION:这里有11个元素,我们把上面的序列前面和后面增加一个0)

    然后,枚举开头O(N),检查O(N)。所以就是平方级算法了,可以通过此题。

    检查可以用KMP的思想(其实我就是用了KMP)。

    /*
    TASK:theme
    LANG:C++
    */
    
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    int n, a[5005], f[5005], ans;
    
    void read()
    {
        int before, x;
        scanf("%d", &n);
        before = 0;
        for (int i = 0; i < n; ++i)
        {
            scanf("%d", &x);
            a[i] = x - before;
            before = x;
        }
        a[n] = -before;
    }
    
    int main()
    {
        freopen("theme.in", "r", stdin);
        freopen("theme.out", "w", stdout);
        read();
        ans = 0;
        for (int fr = n - 3; fr >= 5; --fr)
        {
            memset(f, 0, sizeof(f));
            f[0] = f[1] = 0;
            for (int i = fr + 1; i <= n; ++i)
            {
                int j = f[i - fr];
                while (j != 0 && a[i] != a[fr + j]) j = f[j];
                f[i - fr + 1] = (a[i] == a[fr + j]) ? j + 1 : 0;
            }
            int j = 0;
            for (int i = 0; i < fr - 1; ++i)
            {
                while (j != 0 && a[i] != a[fr + j]) j = f[j];
                if (a[i] == a[fr + j])
                {
                    j++;
                    ans = max(ans, j + 1);
                }
            }
        }
        if (ans < 5) printf("0
    ");
        else printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    关于接口、抽象、普通类之间的选择
    对象与运行时内存
    maven
    ClassLoader
    股票数据调用示例代码php
    猫否股票策略十三篇-1.选股不重要,重在选时
    老枪的59条制胜法则
    今日趁利好出货又套人无数
    判断趋势的最佳指标---趋势大师(源码、主图、附图、说明、无未来、通达信)
    泰禾集团最近走势诡异,小心被机构戏耍了
  • 原文地址:https://www.cnblogs.com/albert7xie/p/5002674.html
Copyright © 2011-2022 走看看