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
6
tanbirahmed
shahriarmanzoor
monirulhasan
syedmonowarhossain
sadrulhabibchowdhury
mohammadsajjadhossain
Sample Output
Case 1: 5
Case 2: 7
Case 3: 6
Case 4: 8
Case 5: 8
Case 6: 8

问题链接:UVA10739 String to Palindrome
问题简述:给定一个字符串,可以增加一个字母或者删除一个字母,或者替换任何一个字母,都算作一次操作,计算最少需要几次操作可以成为回文串。
问题分析
    三种操作是:删除一个字符,添加一个字符,替换一个字符。
    本题提供两种解法,一是记忆化搜索,二是DP。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(记忆化搜索)如下:

/* UVA10739 String to Palindrome */#include <bits/stdc++.h>using namespace std;const int N = 1000;
char s[N + 1];
int dp[N][N];int dfs(int left, int right)
{if(dp[left][right] != -1) return dp[left][right];if(left >= right) return dp[left][right] = 0;if(s[left] == s[right]) return dp[left][right] = dfs(left + 1, right - 1);else {  // s[left] != s[right],删除字符dp[left][right] = dfs(left + 1, right); // 删除左边字符dp[left][right] = min(dp[left][right], dfs(left, right - 1)); // 删除右边字符dp[left][right] = min(dp[left][right], dfs(left + 1, right - 1));    // 删除左右字符return dp[left][right] += 1;}
}int main()
{int t;scanf("%d", &t);for(int k = 1; k <= t; k++) {scanf("%s", s);memset(dp, -1, sizeof(dp));printf("Case %d: %d\n", k, dfs(0, strlen(s) - 1));}return 0;
}

AC的C++语言程序(DP)如下:

/* UVA10739 String to Palindrome */#include <bits/stdc++.h>using namespace std;const int N = 1000;
char s[N + 1];
int dp[N][N];int main()
{int t;scanf("%d", &t);for(int k = 1; k <= t; k++) {scanf("%s", s);int len = strlen(s);for(int i = 0; s[i]; i++) dp[i][i] = 0;for(int i = len - 1; i >= 0; i--)for(int j = i + 1; j < len; j++)if(s[i] == s[j]) dp[i][j] = dp[i + 1][j - 1];else {dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]);dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]) + 1;}printf("Case %d: %d\n", k, dp[0][len - 1]);}return 0;
}

UVA10739 String to Palindrome【记忆化搜索+DP】相关推荐

  1. [蓝桥杯][算法提高VIP]夺宝奇兵(记忆化搜索||DP)

    题目描述 在一座山上,有很多很多珠宝,它们散落在山底通往山顶的每条道路上,不同道路上的珠宝的数目也各不相同.下图为一张藏宝地图: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 " ...

  2. 【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)

    题干: 小乐乐一天天就知道玩,这一天又想玩象棋. 我们都知道马走日. 现在给定一个棋盘,大小是n*m,把棋盘放在第一象限,棋盘的左下角是(0,0),右上角是(n - 1, m - 1); 小乐乐想知道 ...

  3. BZOJ2246 [SDOI2011]迷宫探险 【记忆化搜索dp + 概率】

    题目 输入格式 输出格式 仅包含一个数字,表示在执行最优策略时,人物活着走出迷宫的概率.四舍五入保留3位小数. 输入样例 4 3 3 2 .$. A#B A#C @@@ 143 37 335 85 9 ...

  4. CF1398D Colored Rectangles (记忆化搜索DP)

    数据范围只有200,所以我们可以用O(n3)O(n^3)O(n3)的暴力DP 闫氏DP分析法用着是真的爽 #include<cstdio> #include<algorithm> ...

  5. BZOJ3769:BST again(记忆化搜索DP)

    Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第一行一个整数T,表示数据组数. 以下T行,每行2个整数n和h ...

  6. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  7. 【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

    题干: FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimens ...

  8. codeforces 1089A.lice the Fan(记忆化搜索dp)

    题解:主要在于第五局的时候比赛次数不一样了,所以多开一维专门代表时候打满了5场. dp[0]代表没打满五场,1代表打满了5场] [胜了wa场] [败了wb场] [共赢了na个球] [共输了nb个球]. ...

  9. HDU1978 记忆化搜索

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

最新文章

  1. 干货满满:详解四组遍历数组
  2. setsockopt()函数功能介绍
  3. java对象数组覆盖_java – 如何覆盖RAML 1.0中的对象数组属性类型
  4. Object类、常用API
  5. element 搜索匹配_分享一个element-ui级联选择器的搜索问题,顺便问下有没有解决方案。...
  6. Apache Struts 修复 OGNL 技术中可能存在的 RCE 缺陷
  7. 双击计算机桌面误删,手提电脑双击界面自动删除是哪里问题
  8. div显示图片_图片按照百分比显示部分,不变形
  9. u-boot源码汇编段简要分析
  10. 管理新语:会议与问题的关系
  11. 第一次制作中秋博饼小游戏的心得与吐槽(软件工程)
  12. 龙之谷手游服务器修改,龙之谷手游互通区一览 5月12日部分服务器数据互通公告...
  13. 第六章:纯策略纳什均衡
  14. theHarvester使用
  15. 词性标注集句和句法分析标注集
  16. java hashmap 随机_有没有办法在Java中随机获取HashMap的值?
  17. 通过okHttpUtils实现文件的上传下载
  18. iOS App 安装包瘦身指南
  19. Cisco ASA/FTD未授权文件删除漏洞简要分析
  20. Python:实现double factorial iterative双阶乘迭代算法(附完整源码)

热门文章

  1. Custom UDP Packet Wrapper
  2. GDAL的一个BUG
  3. Html状态属性,html一些对象属性的介绍
  4. AcheGesture 简介(使用方法 / 中文教程)
  5. 高级着色语言HLSL入门(1)
  6. 55个javascript经典用法
  7. Hadoop hdfs编程案例和java交互
  8. . 在第一代计算机时代 编程采用,在第一代计算机时代,编程采用什么语言
  9. 怎样更改itunes备份位置_iphone备份太大,严重挤占C盘空间,怎么把备份放在其他的硬盘?...
  10. .net @什么意思_.NET和F#周报2019-4 各地微软技术俱乐部汇总 ML.NET 0.10