10453 Make Palindrome (dp)
Problem A
Make Palindrome
Input: standard input
Output: standard output
Time Limit: 8 seconds
By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.
Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length 'n', no more than (n-1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome "abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are allowed to insert characters at any position of the string.
Input
Each input line consists only of lower case letters. The size of input string will be at most 1000. Input is terminated by EOF.
Output
For each input print the minimum number of characters and such a palindrome seperated by one space in a line. There may be many such palindromes. Any one will be accepted.
Sample Input
- abcdaaaaabcaababababaabababapqrsabcdpqrs
Sample Output
- 3 abcdcba0 aaaa2 abcba1 baab0 abababaabababa9 pqrsabcdpqrqpdcbasrqp
题意:给定字符串。可以在任意位置增添字符,求最少步骤生成回文串。以及生成的串
思路:
和这题一样。http://blog.csdn.net/accelerator_/article/details/11542037。
多开一个vis数组记录状态转移方式便于输出。
代码:
- #include <stdio.h>
- #include <string.h>
- const int MAXN = 1005;
- char sb[MAXN];
- int dp[MAXN][MAXN], vis[MAXN][MAXN], n, i, j;
- void print(int i, int j) {
- if (i == j) {
- printf("%c", sb[i]);
- return;
- }
- if (i > j)
- return;
- if (vis[i][j] == -1) {
- printf("%c", sb[i]);
- print(i + 1, j - 1);
- printf("%c", sb[j]);
- }
- else if (vis[i][j] == 0) {
- printf("%c", sb[i]);
- print(i + 1, j);
- printf("%c", sb[i]);
- }
- else if (vis[i][j] == 1) {
- printf("%c", sb[j]);
- print(i, j - 1);
- printf("%c", sb[j]);
- }
- }
- int main() {
- while (gets(sb) != NULL) {
- n = strlen(sb);
- for (i = n - 1; i >= 0; i --) {
- for (j = i + 1; j < n; j ++) {
- if (sb[i] == sb[j]) {
- dp[i][j] = dp[i + 1][j - 1];
- vis[i][j] = -1;
- }
- else {
- if (dp[i + 1][j] < dp[i][j - 1]) {
- dp[i][j] = dp[i + 1][j] + 1;
- vis[i][j] = 0;
- }
- else {
- dp[i][j] = dp[i][j - 1] + 1;
- vis[i][j] = 1;
- }
- }
- }
- }
- printf("%d ", dp[0][n - 1]);
- print(0, n - 1);
- printf("\n");
- }
- return 0;
- }
10453 Make Palindrome (dp)的更多相关文章
- [POJ1159]Palindrome(dp,滚动数组)
题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
- POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...
- POJ 3280 Cheapest Palindrome (DP)
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...
- [luoguP2890] [USACO07OPEN]便宜的回文Cheapest Palindrome(DP)
传送门 f[i][j] 表示区间 i 到 j 变为回文串所需最小费用 1.s[i] == s[j] f[i][j] = f[i + 1][j - 1] 2.s[i] != s[j] f[i][j] = ...
- uva 10453 - Make Palindrome(dp)
题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...
- 区间DP UVA 10453 Make Palindrome
题目传送门 /* 题意:问最少插入多少个字符使得字符串变成回文串 区间DP:dp[i][j]表示[l, r]的字符串要成为回文需要插入几个字符串,那么dp[l][r] = dp[l+1][r-1]; ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- 2014百度之星资格赛 1004:Labyrinth(DP)
Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- python模块学习之random
模块源码: Source code: Lib/random.py 文档:http://docs.python.org/2/library/random.html 常用方法: random.random ...
- Eclipse中设置注释、日期等信息
在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的,例如其中author,datetime等属性值. 我们可以在Eclipse 中进行设置自己希望显示的注释信息 ...
- Python load json file with UTF-8 BOM header - Stack Overflow
Python load json file with UTF-8 BOM header - Stack Overflow 3 down vote Since json.load(stream) use ...
- 使用GridBagLayout控制行列的高度和宽度
摘自http://bbs.csdn.net/topics/340189065使用GridBagLayout控制行列的高度和宽度 gridwidth 指定组件显示区域的某一行中的单元格数. 默认值1,水 ...
- java面试题集1
一:单选题 下列哪一种叙述是正确的(D )A. abstract修饰符可修饰字段.方法和类B. 抽象方法的body部分必须用一对大括号{ }包住C. 声明抽象方法,大括号可有可无D. 声明抽象方法不可 ...
- android假设重写onDraw实现一个相似TextView能够显示表情和链接的控件(一)
先看效果图: 写一个超连接支持的对象: /**作为超连接显示的对象*/ public class LinkInfo implements Comparable<LinkInfo>{ pri ...
- 说说ShellExecuteEx
今天来说说ShellExecuteEx这个函数,先翻译MSDN,然后看个样例. ShellExecuteEx Function 对指定应用程序运行某个操作 语法: BOOL ShellExecuteE ...
- RMAN数据库恢复 之归档模式有(无)备份-丢失数据文件的恢复
1.归档模式有备份,丢失数据文件的恢复归档模式有备份,不管丢失什么数据文件,直接在RMAN下RESTOER--->RECOVER--->OPEN即可. RMAN> STARUP MO ...
- 3GP文件格式研究
需要看的文档 http://www.3gpp.org/ftp/Specs/archive/26_series/ 3GPP TS 26.233 3GPP TS 26.243 3GPP TS 26.244 ...
- ECSHOP用户评论
可以不需要审核吗?现在的用户评论要审核才能显示 ,我需要不用审核就可以显示可以么? 在论坛上看见这个问题,顺便就记录下来吧. 这个是可以的,下面是操作步骤 后台->系统设置->商店设置-& ...