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

abcd
aaaa
abc
aab
abababaabababa
pqrsabcdpqrs

Sample Output

3 abcdcba
0 aaaa
2 abcba
1 baab
0 abababaabababa
9 pqrsabcdpqrqpdcbasrqp

相信看过我博客中另外两篇回文串的区间dp,这道题已经完全没任何难度。只是这道题多了一个输出回文串的要求。我用了road数组,模仿的是LCS中路径记录数组。

打印的时候又开了两个数组L,R用来存储左边字符和右边字符,帮助打印,其实完全可以利用递归栈的后进先出的性质直接打印,如果有兴趣可以自己尝试写下代码。当然这题还可以不用road数组,直接比较dp数组也可以完成任务。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#define Maxn 1010
using namespace std;

char s[Maxn];
int dp[Maxn][Maxn],road[Maxn][Maxn];
char L[Maxn],R[Maxn];
int lf,rf;
void print(int b,int e){
    if(b==e) {L[lf++]=s[b];return;}
    if(b+1==e){
        if(s[b]==s[e]) {L[lf++]=s[b];L[lf++]=s[b];}
        else {L[lf++]=s[e];L[lf++]=s[b];L[lf++]=s[e];}
        return;
    }
    if(road[b][e]==0){
        L[lf++]=s[b];
        R[rf++]=s[e];
        print(b+1,e-1);
    }
    else if(road[b][e]==1){
        L[lf++]=s[e];
        R[rf++]=s[e];
        print(b,e-1);
    }
    else{
        L[lf++]=s[b];
        R[rf++]=s[b];
        print(b+1,e);
    }
}
int main()
{
    while(~scanf("%s",s)){
        int n=strlen(s);
        for(int i=0;i<n;i++){
            dp[i][i]=0;
            if(s[i]==s[i+1]) dp[i][i+1]=0;
            else dp[i][i+1]=1;
        }
        for(int l=2;l<n;l++)
            for(int i=0,j=l;j<n;i++,j++)
                if(s[i]==s[j]){
                    dp[i][j]=dp[i+1][j-1];
                    road[i][j]=0;
                }
                else if(dp[i][j-1]<dp[i+1][j]){
                    dp[i][j]=dp[i][j-1]+1;
                    road[i][j]=1;
                }
                else{
                    dp[i][j]=dp[i+1][j]+1;
                    road[i][j]=2;
                }
        printf("%d ",dp[0][n-1]);
        lf=rf=0;
        print(0,n-1);
        for(int i=0;i<lf;i++)
            printf("%c",L[i]);
        for(int i=rf-1;i>=0;i--)
            printf("%c",R[i]);
        puts("");
    }
	return 0;
}

你可能感兴趣的:(uva 10453 - Make Palindrome)