USaco 5.1 Musical Themes(KMPまたはDP)


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] 
    題意:数の列をあげて、数の列の中で差の関係の同じ2つの子の列の最大の長さを聞きます
    解析:最初に思いついた方法は隣接する2つの数を減らすことであり、差のあるシーケンスの2つの同じサブシーケンスを見つければよい.
    次に、あるサブシーケンスを列挙し、KMPで別のサブシーケンスを探し、最後に問題解を見て、この方法がT_を弱く爆発させたことを発見した.T
    問題の解き方はDPです
    次のように
    f[i,j]を2段の繰返しメロディーの先頭がiとjのときの最大長とする.
    則:f[i,j]=min(f[i-1,j-1]+1,i-j)s[i]=s[j]の場合
    =1 s[i]<>s[j]の場合
    DPメソッド:
    /* ID: 15114582 PROG: theme LANG: C++ */ #include<cstdio> #include<iostream> using namespace std; const int mm=5555; int a[mm],s[mm],f[mm]; int i,j,k,n,ans; int main() { freopen("theme.in","r",stdin); freopen("theme.out","w",stdout); while(~scanf("%d",&n)) { for(i=0;i<n;++i) scanf("%d",&a[i]); for(i=1;i<n;++i) s[i]=a[i-1]-a[i]; ans=0; for(i=1;i<n;++i) for(j=n-1;j>i+ans;--j) { if(s[i]==s[j])f[j]=min(f[j-1]+1,j-i+1); else f[j]=0; ans=max(ans,f[j]+1); } if(ans<5)ans=0; printf("%d
    ",ans); } return 0; }