UVA 10739 String to Palindrome(dp)
Problem H
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 |
题意:给定一个字符串,可以进行添加,删除,和替换操作,求最少操作数使得该串变成回文串。
思路:i,j作为字符串的头尾。添加和删除操作其实是一样的。所以只需要考虑2种状态的转移了。
状态转移方程如果str[i] == str[j]则满足回文不用转换。dp[i][j] = dp[i + 1][j - 1]
如果不相等:dp[i][j] = min(min(dp[i + 1][j], dp[i][j - 1]), dp[i + 1][j - 1]) + 1
由于是递推。所以要从后往前。
代码:
#include <stdio.h>
#include <string.h> int t, i, j, dp[1005][1005], len;
char sb[1005]; int min(int a, int b) {
return a < b ? a : b;
} int main() {
scanf("%d%*c", &t);
int tt = 1;
while (t --) {
memset(dp, 0, sizeof(dp));
gets(sb);
len = strlen(sb);
for (i = len - 1; i >= 0; i --) {
for (j = i + 1; j < len; j ++) {
if (sb[i] == sb[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", tt ++, dp[0][len - 1]);
}
return 0;
}
UVA 10739 String to Palindrome(dp)的更多相关文章
- 区间DP UVA 10739 String to Palindrome
题目传送门 /* 题意:三种操作,插入,删除,替换,问最少操作数使得字符串变成回文串 区间DP:有一道类似的题,有点不同的是可以替换,那么两端点不同的时候可以替换掉一个后成回文, 即dp[j+1][k ...
- UVA 10739 String to Palindrome(动态规划 回文)
String to Palindrome 题目大意:给出一个字符串s,现在可以进行3种操作(添加字母,删除字母,替换字母),将其变成回文串,求出最少的操作次数.比如abccda,可以用删除操作,删除b ...
- uva 10739【基础(区间)dp】
Uva 10739 题意:给定字符串,可以增加.删除.修改任意字符,问最少经过多少次操作使字符串回文. 题解:定义dp[l][r]表示把从l到r的子串Sl...Sr变成回文串需要操作的最少次数.字符可 ...
- uva 10453 - Make Palindrome(dp)
题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...
- 区间DP UVA 1351 String Compression
题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...
- uva 11475 - Extend to Palindrome(KMP)
option=com_onlinejudge&Itemid=8&category=506&page=show_problem&problem=2470" ta ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- uva live 4394 String painter 间隔dp
// uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...
随机推荐
- php 判断是否登录
<?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function _before_index(){ //做 ...
- NYoj-街区最短路径问题
街区最短路径问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 一个街区有非常多住户,街区的街道仅仅能为东西.南北两种方向. 住户仅仅能够沿着街道行走. 各个街道之 ...
- TCP/IP之TCP连接的建立与中止状态分析
TCP连接的建立可以简单的称为三次握手,而连接的中止则可以叫做四次握手. 1.连接的建立: c端发起请求同步(用SYN段等于1的TCP报文),确认某个端口是否监听: s端应答(用ACK段等于1的TCP ...
- 计算闰年_winform
新建窗体应用程序(如下),新建控件label1,label2,label3,textBOX1,button1,button2 label1的Text属性改为“计算闰年演示” label2的Text属性 ...
- Java Interface,反射
先看代码, package reflect; //Class Word public class Word implements OfficeAble{ public static void main ...
- POJ 3528 求三维凸包表面积
也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...
- (Problem 21)Amicable numbers
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into ...
- Tcl语言笔记之一
1,一个TCL脚本可以包含一个或多个命令.命令之间必须用换行符或分号隔开 2,置换 substitution %set y x+100 // ...
- centos php扩展开发流程
原文:centos php扩展开发流程 一.安装php centos 默认 yum 安装 php 版本为 5.3, 很多php框架基本上要求5.4以上版本,这时候不能直接 用 yum install ...
- 修复ubuntu播放wmv等视频没有声音问题
1. Mplayer or SMplayer 1.1 原因: 很可能是你没有安装 w32codes 1.2 解决方法: (1)下载 w32codes 官方站点 all-20110131.tar.bz2 ...