USACO 5.1 music theme

很容易注意到:对原序列相邻两项做差(x[i]-x[i-1]),然后题目要求就变成了球最长重复子串。。听说可以用后缀数组,orz。。还不会= =,

偶用的傻傻的DP。。

dp[i][j] (i<j)=max(1,   min(dp[i-1][j-1]+1 (if num[i]==num[j]), j-i));

 

/*
ID: zlqest11
LANG: C++
TASK: theme
*/

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 5010;

int n, num[N], dp[2][N];

int main()
{
int ans = 1;
freopen("theme.in", "r", stdin);
freopen("theme.out", "w", stdout);
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", num+i);
for(int i = n; i > 1; i--){
num[i] -= num[i-1];
}
int flag = 0;
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++)
dp[flag][i] = 1;
for(int i = 2; i <= n; i++){
flag ^= 1;
for(int j = i+1; j <= n; j++){
dp[flag][j] = 1;
if(num[i] == num[j])
dp[flag][j] = dp[flag^1][j-1]+1;
if(dp[flag][j] > j-i)
dp[flag][j] = j-i;
ans = max(ans, dp[flag][j]);
}
}
if(ans < 5) ans = 0;
printf("%d\n", ans);
return 0;
}



posted on 2012-01-26 21:00  Moon_1st  阅读(291)  评论(0编辑  收藏  举报

导航