uva 10453 Make Palindrome(DP)
来源:互联网 发布:matlab trapz函数算法 编辑:程序博客网 时间:2024/05/16 05:53
Problem B : Make Palindrome
From:UVA, 10453
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
<br />
#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;const int maxn = 1105;int dp[maxn][maxn];int vis[maxn][maxn] = {0};int v;//string ini;char ini[maxn];int DP(int i , int j){//cout << i << " " << j << endl;if(i == j) return 0;if(i > j) return 0;if(vis[i][j] == v) return dp[i][j];int tem = 0;if(ini[i] == ini[j]){tem = DP(i+1 , j-1);//cout << i << " " << j << "=" << tem.ans << endl;}else{int d1 = DP(i+1 , j) , d2 = DP(i , j-1);if(d1 <= d2){tem = d1+1;}else{tem = d2+1;}//cout << i << " " << j << "=" << tem.ans << endl;}vis[i][j] = v;return dp[i][j] = tem;}void print(int i , int j){if(i == j){printf("%c" , ini[i]);return;}if(i > j){printf("");return;}if(ini[i] == ini[j]){printf("%c" , ini[i]);print(i+1 , j-1);printf("%c" , ini[i]);}else{if(DP(i+1 , j) <= DP(i , j-1)){printf("%c" , ini[i]);print(i+1 , j);printf("%c" , ini[i]);}else{printf("%c" , ini[j]);print(i , j-1);printf("%c" , ini[j]);}}}int main(){v = 1;while(scanf("%s" , ini) != EOF){printf("%d ",DP(0 , strlen(ini)-1));print(0 , strlen(ini)-1);printf("\n");//printf("%d %s\n" , ans.sum , ans.ans.c_str());v++;}return 0;}<br /><span id="_xhe_temp" width="0" height="0"><br /></span>
- UVA 10453 Make Palindrome(DP)
- uva 10453 Make Palindrome(DP)
- UVA 10453-Make Palindrome(DP)
- uva 10453 - Make Palindrome(dp)
- Make Palindrome - UVa 10453 dp
- uva 10453 - Make Palindrome (区间dp,记忆化搜索)
- uva 10453 Make Palindrome (区间DP + 递归输出)
- UVa 10453 - Make Palindrome 字符串dp
- UVA 10453 Make Palindrome(区间简单DP)
- UVA - 10453 Make Palindrome 区间DP
- uva 10453 Make Palindrome
- UVA 10453 Make Palindrome
- UVA 10453 Make Palindrome
- UVA 10453 Make Palindrome
- UVA - 10453 Make Palindrome
- UVa:10453 Make Palindrome
- uva 10453 - Make Palindrome
- UVA 10453 - Make Palindrome
- 没什么……刚开通
- G729
- 使用QQ邮箱“邮我”组件,方便他人快速给你发邮件
- LeetCode OJ:Binary Tree Inorder Traversal
- Chrome妙用
- uva 10453 Make Palindrome(DP)
- 数据结构和算法
- 最常用的设计模式---组合模式(C++实现)
- android自定义布局中的平滑移动 .
- CyclicBarrier介绍
- 袁腾飞讲述西方古代哲学思想
- 欲望
- 线程学习之-合并线程
- 修改Chrome默认搜索引擎为Google.com