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 ...
随机推荐
- Dropdownlist的onchange事件应用
function selectDpList(dp) { var sIndex = dp.selectedIndex;//返回选中是第几项 0,1.... var sText = dp.options[ ...
- MySQL库目录下db.opt文件的作用
细心的朋友可能会发现有时候在某些库目录下有个 db.opt 文件,那这个文件是干什么用的呢?如果你用vi等编辑器打开看的话,内容很简单,是用来记录该库的默认字符集编码和字符集排序规则用的.也就是说如果 ...
- mCustomScrollbar的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- JS闭包的概念
原文地址:http://zhidao.baidu.com/link?url=f81iaijX6nzY99Wz43v-p_qZEn4cCaomT4LD6NH5jVtI0yK2V76VJWefih51vA ...
- openstack configure
<一,nova.conf配置文件配置 hypervisors compute_driver = 值> 1,kvm/qemu Hypervisor OpenStack nova comput ...
- Unity 接MM横屏闪退的原因
=.=研究了1天接SDK到处都在报错,于是使用logcat查看原因截取到这样的Exception. call to OpenGL ES API withno current context(logge ...
- Struts2+Spring集成合并
前边单独总结了Struts2,Spring和Ibaits框架了,那么怎么结合使用呢?这次先来看一下Sturts2和Spring的集成合并.其实挺简单的,就是导入各自的jar包以及连接彼此的jar包,分 ...
- IOS Custom NavigationItem --写titleView
//先自己写一个titleView UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];//all ...
- ubuntu 14.04 chromium 设备adobe flash player(亲测可行)
首先,根据浏览器提示下载Adobe Flash Player 插入 install_flash_player_11_linux.x86_64.tar.gz;然后使用sudo tar -xzvf ins ...
- hdu2095 像水题的不错题 异或运算
异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...