UVA 10453 Make Palindrome

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 abcdcba
0 aaaa
2 abcba
1 baab
0 abababaabababa
9 pqrsabcdpqrqpdcbasrqp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1100;
char str[maxn];
int dp[maxn][maxn];
int len;
void output(int i,int j)
{
//    cout<<"1111  "<<endl;
     if(i>j)  return ;
     if(i==j)
     {
        printf("%c",str[i]);
        return ;
     }
     if(str[i]==str[j])
     {
         printf("%c",str[i]);
         output(i+1,j-1);
         printf("%c",str[i]);
     }
     else if(dp[i][j]==dp[i+1][j]+1)
     {
         printf("%c",str[i]);
         output(i+1,j);
         printf("%c",str[i]);
     }
     else
     {
         printf("%c",str[j]);
         output(i,j-1);
         printf("%c",str[j]);
     }
}
int solve()
{
    memset(dp,0,sizeof(dp));
    for(int l=2;l<=len;l++)
    {
        for(int i=0;i+l-1<len;i++)
        {
            int j=i+l-1;
            dp[i][j]=INT_MAX;
            if(str[i]==str[j])
                dp[i][j]=dp[i+1][j-1];
            else
                dp[i][j]=min(dp[i][j],min(dp[i+1][j]+1,dp[i][j-1]+1));
        }
    }
}
int main()
{
    while(~scanf("%s",str))
    {
        len=strlen(str);
        solve();
        printf("%d ",dp[0][len-1]);
        output(0,len-1);
        puts("");
    }
    return 0;
}
时间: 2024-10-13 08:01:08

UVA 10453 Make Palindrome的相关文章

UVA 10453 Make Palindrome(区间简单DP)

题意:给出一个字符串A,求出需要至少插入多少个字符使得这个字符串变成回文串. 思路:设dp[i][j]为使区间[i, j]变成回文串所需要的最少字符个数. 1.A[i] == A[j的情况]那么dp[i][j] = min(dp[i][j], dp[i + 1][j -1]); 2.或者在第j个位置插入一个字符A[i], dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1); 3.或者在第i个位置插入一个字符A[j], dp[i][j] = min(dp[i][j

uva 10453 Make Palindrome (区间DP + 递归输出)

uva 10453 Make Palindrome 题目大意:给出一段字符串,要求求出最少加入几个字符(任意位置),可以让该字符串变成会问字符串,并输出修改以后的回文字符串. 解题思路:dp[i][j]代表了将该字符串从第i位到第j位变成回文字符串最少要添加的字符.当S[i]==S[j],dp[i][j]=dp[i+1][j?1]当S[i]!=S[j],dp[i][j]=min(dp[i+1][j],dp[i][j?1])+1,在DP的过程中记录对该区间的操作类型,最后递归输出. #includ

UVA 10453 十七 Make Palindrome

Make Palindrome Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10453 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 char str[1005]; 7 in

uva 10617 Again Palindrome (DP)

uva 10617 Again Palindrome 题目大意:给出一段字符串,可进行删除操作,可以删除任意位置任意个数(可以是0)的字符.问,进行删除操作使原本字符串变成回文字符串,有几种方式. 解题思路: dp[i][j] = 1 (i == j),单独一个字符也是回文字符串 s[i] != s[j]时, dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1], dp[i + 1][j] 和 dp[i][j + 1]的公共部分dp[

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p

UVa 10617 Again Palindrome(回文串区间DP)

UVa 10617 Again Palindrome(经典回文串区间DP) 题意: 给定一个字符串s,对s进行删除操作,使得剩下的子串是回文字符串,问最多有多少种这种子串. 思路: 涉及到回文字符串,首先要想到的肯定是区间DP,如何写出状态转移方程? 直接从题意切入:dp[i, j]表示区间[i, j]最多有多少个这样的子串. 1. s[i] == s[j] 去掉s[i],则一个子问题就是dp[i+1, j]; 去掉s[j],另一个子问题就是dp[i, j-1]; 显然这两个子问题是会有重叠的,

Make Palindrome UVA - 10453

题意:添加尽量少的字符使得s串成为回文串,并输出这样得解. 题解:dp[ i ][ j ]表示i~j串需要添加的最少字符. 当s[ i ]==s[ j ]时,dp[ i ][ j ]=dp[ i +1 ][ j - 1 ]; 当s[ i ]! =s[ j ]时,dp[ i ][ j ]=min( dp[ i ][ j ],min(dp[ i+1 ][ j ],dp[ i ][ j - 1 ]+1); 头疼的是打印解. (1)顺着刷 1 void solve(){ 2 for(int i=0;i<

Uva 11151 - Longest Palindrome

A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-palindromic string, you can al

Uva 10617 Again Palindrome(区间dp)

Again Palindromes Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, Z, TOT and MADAM are palindromes, but