题目传送门
1 /*
2 题意:三种操作,插入,删除,替换,问最少操作数使得字符串变成回文串
3 区间DP:有一道类似的题,有点不同的是可以替换,那么两端点不同的时候可以替换掉一个后成回文,
4 即dp[j+1][k-1] + 1,还有这道题没有要求打印
5 */
6 /************************************************
7 * Author :Running_Time
8 * Created Time :2015-8-17 15:45:22
9 * File Name :UVA_10739.cpp
10 ************************************************/
11
12 #include <cstdio>
13 #include <algorithm>
14 #include <iostream>
15 #include <sstream>
16 #include <cstring>
17 #include <cmath>
18 #include <string>
19 #include <vector>
20 #include <queue>
21 #include <deque>
22 #include <stack>
23 #include <list>
24 #include <map>
25 #include <set>
26 #include <bitset>
27 #include <cstdlib>
28 #include <ctime>
29 using namespace std;
30
31 #define lson l, mid, rt << 1
32 #define rson mid + 1, r, rt << 1 | 1
33 typedef long long ll;
34 const int MAXN = 1e3 + 10;
35 const int INF = 0x3f3f3f3f;
36 const int MOD = 1e9 + 7;
37 int dp[MAXN][MAXN];
38 char str[MAXN];
39
40 int work(void) {
41 memset (dp, 0, sizeof (dp));
42 int len = strlen (str);
43 for (int i=2; i<=len; ++i) {
44 for (int j=0; j+i-1<len; ++j) {
45 int k = j + i - 1;
46 int &res = dp[j][k] = INF;
47 if (str[j] == str[k]) res = dp[j+1][k-1];
48 res = min (res, min (dp[j+1][k], min (dp[j][k-1], dp[j+1][k-1])) + 1);
49 }
50 }
51 return dp[0][len-1];
52 }
53
54 int main(void) { //UVA 10739 String to Palindrome
55 int T, cas = 0; scanf ("%d", &T);
56 while (T--) {
57 scanf ("%s", str);
58 printf ("Case %d: %d
", ++cas, work ());
59 }
60
61 return 0;
62 }