【原题】
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)
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]
【译题】
描述
我们用N(1 <= N <=5000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,每个数表示钢琴上的一个键。很不幸这种表示旋律的方法忽略了音符的时值,但这项编程任务是关于音高的,与时值无关。
许多作曲家围绕一个重复出现的“主题”来构建乐曲。在我们的乐曲表示法中,“主题”是整个音符序列的一个子串,它需要满足如下条件:
⒈长度至少为5个音符
⒉在乐曲中重复出现(可能经过转调,见下)
⒊重复出现的同一主题不能有公共部分。
“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值。 给定一段乐曲,计算其中最长主题的长度(即音符数)。
本题时限为1秒钟!
[编辑]格式
PROGRAM NAME: theme
INPUT FORMAT
输入文件的第一行包含整数N。下面的每一行(最后一行可能除外)包含20个整数,表示音符序列。最后一行可能少于20个音符。
OUTPUT FORMAT
输出文件应只含一个整数,即最长主题的长度。如果乐曲中没有主题,那么输出0。
[编辑]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
[编辑]SAMPLE OUTPUT (file theme.out)
5
(这个长度为5的主题是输入文件中第一行的最后5个音符和第二行开头5个音符)
【分析】太强了!原来想用二分枚举加验证,但是对于验证,我只能想出n^3的暴力判断,根本就过不了的。打开NOCWO的题解,一篇有关于最长公共子串的DP方法跳入了我的眼帘。这个方法不错!
【代码1】
#include<stdio.h>
#include<iostream>
using namespace std;
int a[5005],f[5005][5005],i,j,n,ans;
int main()
{
freopen("theme.in","r",stdin);
freopen("theme.out","w",stdout);
scanf("%ld",&n);
for (i=1;i<=n;i++)
scanf("%ld",&a[i]);
for (i=1;i<=n;i++)
for (j=i;j<=n;j++)
{
if (a[i]-a[i-1]==a[j]-a[j-1])
f[i][j]=max(f[i][j],f[i-1][j-1]+1);
if (f[i][j]>ans)
ans=f[i][j];
}
if (ans<4) ans=-1;printf("%ld",ans+1);
return 0;
}
【注释】在比较的时候有点巧妙,因为音调是可以变化的,所以用作差法可以避免这个问题。因为是作差嘛,结果是要加1的。但是很快我就发现这个程序的漏洞了:主题音调是不能重叠的!
【深入分析】避免重复其实也很简单,就是f[i-1][j-1]<j-i-1。只要在画图上随便比划一下就能发现。哦,还有最重要的一点,USACO有坑爹的内存限制,所以还要开滚存!
【代码2(AC)】
/*
PROG:theme
ID:juan1973
LANG:C++
*/
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
int a[5005],f[2][5005],i,j,n,ans,now;
int main()
{
freopen("theme.in","r",stdin);
freopen("theme.out","w",stdout);
scanf("%ld",&n);
for (i=1;i<=n;i++)
scanf("%ld",&a[i]);
for (i=1;i<=n;i++)
{
now=i%2;
memset(f[now],0,sizeof(f[now]));
for (j=i;j<=n;j++)
{
if (a[i]-a[i-1]==a[j]-a[j-1]&&f[now^1][j-1]<j-i-1)
f[now][j]=max(f[now][j],f[now^1][j-1]+1);
if (f[now][j]>ans)
ans=f[now][j];
}
}
if (ans<4) ans=-1;printf("%ld",ans+1);
return 0;
}