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 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3888451.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值