#include <cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 105;
int a[MAXN];
int dp[MAXN][MAXN];
int main() {
int T;
cin>>T;
int cnt=1;
while(T--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
memset(dp,0x3f,sizeof dp);
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++) dp[i][j] = j-i+1;
for(int len=2;len<=n;len++)
{
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
dp[i][j]=min(dp[i][j],dp[i+1][j]+(a[i]!=a[i+1]));
for(int k=i+2;k<=j;k++)
if(a[k]==a[i])
dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);
}
}
printf("Case %d: %d\n",cnt++,dp[1][n]);
}
return 0;
}
[区间dp] lightoj 1422 Halloween Costumes
最新推荐文章于 2020-04-18 21:34:19 发布