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)的更多相关文章

  1. [POJ1159]Palindrome(dp,滚动数组)

    题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...

  2. POJ 3280 Cheapest Palindrome(DP)

    题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...

  3. POJ 3280 Cheapest Palindrome(DP 回文变形)

    题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...

  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 ...

  5. [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] = ...

  6. uva 10453 - Make Palindrome(dp)

    题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...

  7. 区间DP UVA 10453 Make Palindrome

    题目传送门 /* 题意:问最少插入多少个字符使得字符串变成回文串 区间DP:dp[i][j]表示[l, r]的字符串要成为回文需要插入几个字符串,那么dp[l][r] = dp[l+1][r-1]; ...

  8. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  9. 2014百度之星资格赛 1004:Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. UESTC_Frozen Rose-Heads CDOJ 791

    The winter is coming and all the experts are warning that it will be the coldest one in the last hun ...

  2. Unique Binary Search Trees 解答

    Question Given n, how many structurally unique BST's (binary search trees) that store values 1...n? ...

  3. 算法(Algorithm)是什么?

    我们用煎蛋来打个比方.煎蛋的一般步骤是: 1.>取煎锅. 2.>取油. ->我们有油吗? ****有,把油倒入煎锅. ****没有,我们要去买油吗? #########要买,出去买油 ...

  4. 【CF 675D Tree Construction】BST

    题目链接:http://codeforces.com/problemset/problem/675/D 题意:给一个由n个互异整数组成的序列a[],模拟BST的插入过程,依次输出每插入一个元素a[i] ...

  5. The Java™ Tutorials下载地址

    1.The Java™ Tutorials下载地址: http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-200 ...

  6. hdu 3954 Level up(线段树)

    题目链接:hdu 3954 Level up 题目大意:N个英雄,M个等级,初始等级为1,给定每一个等级须要的经验值,Q次操作,操作分两种,W l r x:表示l~r之间的英雄每一个人杀了x个怪物:Q ...

  7. C#整理8——结构体

    结构体:相当于是我们自己定义的一种复杂的类型.int... double float bool char string DateTime 数组类型生活中大部份的对象都是复合型的对象. 如何定义结构体类 ...

  8. 使用AsyncTask实现图片加载

    如上图所示:我们看到的就是使用PrograssDialog进度条和AsyncTask异步任务实现的效果(额,不要看应用名...).下面介绍一下具体的实现流程. 一.首先使用XML布局,布局很简单直接上 ...

  9. MVC不错的学习资料

    MVC不错的学习资料: http://www.cnblogs.com/darrenji/

  10. InvokeRequired方法和Invoke函数

    c#中禁止跨进程直接访问控件,为了解决这个问题,出现了InvokeRequried属性,当一个控件的InvokeRequried属性值为真时,说明有控件外的线程想要访问它.这时便会调用到Invoke方 ...