Uva--10739(区间DP)

2014-08-03 15:33:02

String to Palindrome

Input: Standard Input

Output: Standard Output

Time Limit: 1 Second

In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below:

Here you’d have the ultimate freedom. You are allowed to:

  • Add any character at any position
  • Remove any character from any position
  • Replace any character at any position with another character

Every operation you do on the string would count for a unit cost. You’d have to keep that as low as possible. 

For example, to convert “abccda” you would need at least two operations if we allowed you only to add characters. But when you have the option to replace any character you can do it with only one operation. We hope you’d be able to use this feature to your advantage.

Input

The input file contains several test cases. The first line of the input gives you the number of test cases, T (1≤T≤10). Then T test cases will follow, each in one line. The input for each test case consists of a string containing lower case letters only. You can safely assume that the length of this string will not exceed 1000 characters. 

Output

For each set of input print the test case number first. Then print the minimum number of characters needed to turn the given string into a palindrome.

 

Sample Input                               Output for Sample Input

6
tanbirahmed
shahriarmanzoor
monirulhasan
syedmonowarhossain
sadrulhabibchowdhury
mohammadsajjadhossain

Case 1: 5

Case 2: 7

Case 3: 6

Case 4: 8

Case 5: 8

Case 6: 8

 

思路:大区间的回文依赖于小区间的回文,属于区间DP。但是这和Uva10304的区间DP有所不同,这题是由小区间逐渐延展成大区间的过程,而10304是由一个个小区间搭建成大区间的过程。

dp[i][j] 表示把区间[ i , j ]调成回文所需的最小步骤数:

  如果s[i] == s[j],那么dp[i][j] = dp[i+1][j-1]

  如果s[i] != s[j],那么枚举三种操作,取最小,dp[i][j] = min(dp[i+1][j] , dp[i][j-1] , dp[i+1][j-1])  (其实增删等价)

 1 /*************************************************************************
 2     > File Name: b2.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sat 02 Aug 2014 10:05:28 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int main(){
17     int T;
18     int len;
19     int dp[1005][1005];
20     char s[1005];
21     scanf("%d",&T);
22     for(int t = 1; t <= T; ++t){
23         scanf("%s",s);
24         memset(dp,0,sizeof(dp));
25         len = strlen(s);
26         for(int i = len - 1; i >= 0; --i){
27             for(int j = i + 1; j < len; ++j){
28                 if(i == j)
29                     continue;
30                 if(s[i] == s[j])
31                     dp[i][j] = dp[i + 1][j - 1];
32                 else{
33                     dp[i][j] = min(dp[i + 1][j],min(dp[i][j - 1],dp[i + 1][j - 1])) + 1;
34                 }
35             }
36         }
37         printf("Case %d: %d\n",t,dp[0][len - 1]);
38     }
39     return 0;
40 }

 

posted @ 2014-08-03 15:41  Naturain  阅读(144)  评论(0编辑  收藏  举报