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磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...
随机推荐
- jqGrid一些操作
formatter:function(cellvalue,options,rowObject){} 在格式化行的时候这三个参数 cellvalue行数, options配置信息, rowObject行 ...
- bash: ./configure: 权限不够
linux下运行一个文件时 提示权限不够,说明这个文件没有可执行权限 我们可以在属性里面手动为这个文件加上可执行权限也可以使用命令的方式 命令方式: 例如: bash: ./configure: ...
- vc 获取网页内容(HTTP)
BOOL bResult = FALSE; // 初始化WinInet 环境 HINTERNET hInternet = InternetOpen("CEHTTP", ...
- 简单的iOS抽屉效果
#define screenW [UIScreen mainScreen].bounds.size.width #import "ViewController.h" @interf ...
- javascript 学习随笔3
<html> <head> <script type="text/javascript"> function startTime() { var ...
- mysql版sql助记
新建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; [host 中 使用 % 为通配符, 匹配任意远程主机] 赋权 GRANT p ...
- redis(四)redis与Mybatis的无缝整合让MyBatis透明的管理缓存
redis的安装 http://liuyieyer.iteye.com/blog/2078093 redis的主从高可用 http://liuyieyer.iteye.com/blog/207809 ...
- Arduino 入门程序示例之一排 LED(2015-06-11)
概述 最简单的一个 LED 的实验之后,自然是增加几个 LED,咱排成一排来玩吧.最后,再把一排的 LED 排成一个 8 字来玩——七段数码管. 示例程序 流水灯 第一个出场的肯定是经典的流水灯,也叫 ...
- BZOJ 3359: [Usaco2004 Jan]矩形( dp )
数据范围这么小..怎么乱搞都可以吧... 先排序一遍然后O(n²) dp --------------------------------------------------------------- ...
- 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
再说字符串 一.字符串的编码 字符串的编码是个很令人头疼的问题,由于计算机是美国人发明的,他们很理所当然的认为计算机只要能处理127个字母和一些符号就够用了,所以规定了一个字符占用8个比特(bit)也 ...