題目鏈接:http://poj.org/problem?id=1390&tdsourcetag=s_pctim_aiomsg
題目大意:一個積木遊戲,有連續的n個方塊。相同顏色的方塊連續方塊可以消除。如果該段由k個積木組成。得分爲k*k。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int a[205], b[205], len[205], f[205][205][205];
int DP(int l, int r, int k){
if(r<l) return 0;
if(f[l][r][k]!=-1) return f[l][r][k];
int ans=DP(l, r-1, 0)+(len[r]+k)*(len[r]+k);
for(int i=l; i<r; i++){
if(a[i]==a[r]){
ans=max(ans, DP(l, i, len[r]+k)+DP(i+1, r-1, 0));
}
}
return f[l][r][k]=ans;
}
int main() {
int t, T=1; scanf("%d", &t);
while(t--){
memset(f, -1, sizeof(f));
memset(len, 0, sizeof(len));
int n; scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &b[i]);
}
int tot=1;
a[tot]=b[1], len[1]=1;
for(int i=2; i<=n; i++){
if(b[i]!=b[i-1]){
a[++tot]=b[i]; len[tot]=1;
}
else{
len[tot]++;
}
}
printf("Case %d: %d\n", T++, DP(1, tot, 0));
}
return 0;
}