思路:题目说的是给定一个字符串,然后可以对每个字符修改一次,问最少需要修改多少吃可以使得字符串变成回文字符串。这题的状态显然是很好像的dp[i][j]表示区间[i,j]的字符串修改成回文的需要修改的最少次数,然后就是转移方程了。
1,s[i] = s[j],dp[i][j] = dp[i + 1][j - 1];
2, s[i] != s[j],显然此事需要修改一次,看是修改s[i]还是修改s[j],修改s[i]的话就是dp[i + 1][j],修改s[j]的话就是dp[i][j - 1],当然还有就是d[i + 1][j - 1];取其最小者 + 1;
/*****************************************
Author :Crazy_AC(JamesQi)
Time :2015
File Name :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
inline int Readint(){
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)){
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
char s[1010];
int dp[1010][1010];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int t,icase = 0;
scanf("%d",&t);
while(t--){
scanf("%s",s);
int r = strlen(s);
memset(dp, 0,sizeof dp);
for (int i = r - 1;i >= 0;--i){
for (int j = i + 1;j <= r - 1;++j){
if (s[i] == s[j]) dp[i][j] = dp[i + 1][j - 1];
else dp[i][j] = min(min(dp[i + 1][j],dp[i][j - 1]),dp[i + 1][j - 1]) + 1;
}
}
printf("Case %d: %d\n",++icase,dp[0][r - 1]);
}
return 0;
}