lightoj 1033 区间dp

时间:2022-01-27 15:49:21

题目链接:http://lightoj.com/volume_showproblem.php?problem=1033

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std; const int maxe = ;
const int maxn = ;
const int INF = 0x3f3f3f; int dp[maxn][maxn]; //dp[i][j]表示字符串中从i到j最少要添加的字母个数。 int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int cas=;cas<=T;cas++){
char a[maxn];
scanf("%s",a+);
int N = strlen(a+); //memset(dp,0x3f,sizeof(dp));
for(int i=;i<=N;i++) dp[i][i] = dp[i][i-] = ; for(int i=N;i>=;i--)
for(int j=i+;j<=N;j++){
if(a[i] == a[j]){
dp[i][j] = dp[i+][j-];
}
else{
dp[i][j] = min(dp[i+][j],dp[i][j-]) + ;
}
}
printf("Case %d: %d\n",cas,dp[][N]);
}
}