String to Palindrome

题目大意:给出一个字符串s,现在可以进行3种操作(添加字母,删除字母,替换字母),将其变成回文串,求出最少的操作次数。比如abccda,可以用删除操作,删除b,d两步可变成回文;但如果用替换操作,把b换成d则只需要1步。

分析:刚开始我一直考虑它是否具有最优子结构性质,直到现在,还是不明白为什么可以用动态规划来做,大神若是看见,还望指教。

  由于添加字母和删除字母的效果是一样的,因此我们这里就只进行删除和替换操作。令dp[i][j]表示从第 i 到第 j 个字母变成回文所需要最少的操作数。

  转移方程为:当是s[i]==s[j]时,dp[i][j] = dp[i+1][j-1];此外dp[i][j] = min(dp[i+1][j],dp[i+1][j-1],dp[i][j-1]) + 1;  dp[i+1][j-1]+1 是替换操作,其他两种是删除操作。

  初始条件是:j<=i 时dp[i][j] = 0; 如果只是单一的字母,它本身就是回文

递推代码如下:

 # include<cstdio>
# include<cstring>
# include<iostream>
using namespace std;
char s[];
int dp[][];
int main()
{
int T,cas;
scanf("%d",&T);
for(cas=; cas<=T; cas++)
{
scanf("%s",s);
int len =strlen(s);
int i,j;
for(i=; i<len; i++)
dp[i][i] = ;
for(i=len-; i>=; i--)
for(j=i+; j<len; j++)
{
if(s[i]==s[j])
dp[i][j] = dp[i+][j-];
else
dp[i][j] = min(min(dp[i+][j],dp[i+][j-]),dp[i][j-])+;
}
printf("Case %d: %d\n",cas,dp[][len-]);
}
return ;
}

递归代码如下:

 # include<cstdio>
# include<cstring>
# include<iostream>
using namespace std;
char s[];
int dp[][];
int DP(int x,int y){
if(dp[x][y] != -)
return dp[x][y];
if(y <= x )
return dp[x][y] = ;
if(s[x] == s[y])
dp[x][y] = DP(x+,y-);
else
dp[x][y] = min( min(DP(x+,y),DP(x+,y-)),(DP(x,y-))) + ;
return dp[x][y];
}
int main(){
int T,cas;
scanf("%d",&T);
for(cas=;cas<=T;cas++){
scanf("%s",s);
int len =strlen(s);
memset(dp,-,sizeof(dp));
printf("Case %d: %d\n",cas,DP(,len-));
}
return ;
}

UVA 10739 String to Palindrome(动态规划 回文)的更多相关文章

  1. 区间DP UVA 10739 String to Palindrome

    题目传送门 /* 题意:三种操作,插入,删除,替换,问最少操作数使得字符串变成回文串 区间DP:有一道类似的题,有点不同的是可以替换,那么两端点不同的时候可以替换掉一个后成回文, 即dp[j+1][k ...

  2. UVA 10739 String to Palindrome(dp)

    Problem H String to Palindrome Input: Standard Input Output: Standard Output Time Limit: 1 Second In ...

  3. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  5. CF932G Palindrome Partition(回文自动机)

    CF932G Palindrome Partition(回文自动机) Luogu 题解时间 首先将字符串 $ s[1...n] $ 变成 $ s[1]s[n]s[2]s[n-1]... $ 就变成了求 ...

  6. Codeforces 932G Palindrome Partition - 回文树 - 动态规划

    题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...

  7. UVa 12050 - Palindrome Numbers (回文数)

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, th ...

  8. UVA 11584 Paritioning by Palindromes(动态规划 回文)

    题目大意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串.比如racecar本身就是回文串:fastcar只能分成7个单字母的回文串:aaadbccb最少可分成3个回文串:aaa. ...

  9. Palindrome Numbers UVA - 12050(第几个回文数)

    长度为k的回文串个数有9*10^(k-1) #include <iostream> #include <cstdio> #include <sstream> #in ...

随机推荐

  1. json.net json转换神器

    json.nethttps://json.codeplex.com/ api documenthttp://james.newtonking.com/json/help/index.html#

  2. CentOS上安装FastDFS分布式文件系统

    鱼大自己写的项目简介:http://bbs.chinaunix.net/thread-1920470-1-1.html 架构简介:http://www.programmer.com.cn/4380/ ...

  3. [每日一题] 11gOCP 1z0-053 :2013-10-9 backup with the KEEP option....................................33

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12517603 正确答案:AB 在Oracle 11g中,可以使用backup ….keep ...

  4. oc学习之路----QQ聊天界面

    用到的技术:自定义cell,通知机制,代理模式 transform 1.自定义cell(通过代码自定义cell) ①首先新建一个类继承UITableViewCell ②重写initWithStyle: ...

  5. java常见算法

    1.冒泡排序 public int[] bubbleSort(int arr){ int temp; boolean isOk; for(int i = 0; i < arr.length; i ...

  6. mac下apache启动关闭操作

    1.一般的命令如下: 2.但是这种方法在mac上有的时候不生效 3.根据上面的提示使用下面的方法,可以生效,mac上很多应用都可以采用这种方式启动.关闭应用 sudo launchctl load / ...

  7. linux中配置Java环境

    一. 下载JDK 下载linux版本的jdk32(64) 二. 需要配置的环境变量 1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是 ...

  8. oracl使用DataBase Configuration Assistant创建、删除数据库

    原文:oracl使用DataBase Configuration Assistant创建.删除数据库 可以使用DataBase Configuration Assistant来创建一个心得数据库.Da ...

  9. MAX函数和GROUP BY 语句一起使用的一个误区

    使用MAX 函数和 GROUP 的时候会有不可预料的数据被SELECT 出来.下面举个简单的例子:想知道每个SCOREID 的 数学成绩最高的分数. 表信息:/*DDL Information For ...

  10. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...