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磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...
随机推荐
- asp.net插入sql server 中文乱码问题解决方案
创建数据库的代码---创建promary表 create table promary ( proID int primary key, proName varchar(50) not null ) 出 ...
- 通过加索引对sql语句优化
今天看数据库的时候遇到这样一个SQL语句: select substr(a.djxh,) as id, (a.nd || a.yf) DECL_YM, a.zspm_dm as LEVY_ITEM_I ...
- DTM initialization: failure during startup recovery, retry failed, check segment status (cdbtm.c:1603)
安装greenplum集群出现以下错误: 20160315:13:49:16:025696 gpinitsystem:h95:jason-[INFO]:-Checking configuration ...
- Android百度地图之显示地图
添加地图显示 一.在百度官网下载相关的SDK (网址:http://developer.baidu.com/map/sdkandev-download.htm) 解压下载好的BaiduMap_Andr ...
- php 取小数
- bresenham算法的FPGA的实现1
接着上一篇的 计算实现给出屏幕上任意两个点,求出这两个点之间直线上的所有的点.http://www.cnblogs.com/sepeng/p/4042464.html 这种直接算法的确是被鄙视了 强大 ...
- Week6(10月14日)
Part I:提问 =========================== 1.什么是视图模型?2.我们在留言本中,加入了一个怎样的视图模型?如何处理它? Part II:Ch05 视图模型 === ...
- 用QFileSystemWatcher来监视文件和目录的改变(内部还是使用了timer)
Use Case: 两个程序共享同一个Configuration文件,当一个程序作出改变的时候,需要另外一个程序能够及时响应. 之前其实猜的八九不离十,估计是有一个Timer,然后定时查询Config ...
- WebLech是一个功能强大的Web站点下载与镜像工具
WebLech是一个功能强大的Web站点下载与镜像工具.它支持按功能需求来下载web站点并能够尽可能模仿标准Web浏览器的行为.WebLech有一个功能控制台并采用多线程操作. http://sour ...
- 组件接口(API)设计指南-文件夹
组件接口(API)设计指南-文件夹 组件接口(API)设计指南[1]-要考虑的问题 组件接口(API)设计指南[2]-类接口(class interface) 组件接口(API)设计指南[3]-托付( ...