UVA 10453 make Palindrome

Source: Internet
Author: User

Original question:
By definition palindrome was a string which is not the changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string was a palindrome or not. But it may is not being so easy to generate a palindrome.
Here we'll make a palindrome generator which would take an input string and return a palindrome.
You can easily verify. A string of length n, no more than (n−1) characters is required to make it a palindrome. Consider ' ABCD ' and its palindrome ' ABCDCBA ' or ' abc ' and its palindrome ' ABCBA '. But life isn't so easy for programmers!! We always want optimal cost.
And you had to find the minimum number of characters required to make a given string to a
Palindrome If you is allowed to insert characters at any position of the string.
Input
Each of the input line consists is only from lower case letters. The size of the input string would be in most 1000. Input
is terminated by EOF.
Output
For each input print the minimum number of characters and such a palindrome separated by one space
In a line. There may many such palindromes. Any one would be accepted.
Sample Input
Abcd
Aaaa
Abc
AaB
Abababaabababa
Pqrsabcdpqrs
Sample Output
3 ABCDCBA
0 AAAA
2 ABCBA
1 Baab
0 Abababaabababa
9 PQRSABCDPQRQPDCBASRQP
Effect:
Give you a string, let you randomly insert characters, ask you to insert at least how many characters can make the string into a palindrome, and print out any string.

#include <bits/stdc++.h> using namespace std;
FStream in,out;
int Dp[1001][1001],len;
string S;
    void GetString (int i,int j) {if (i>j) return;
        if (i==j) {cout<<s[i-1];
    Return
        } if (S[i-1]==s[j-1]) {cout<<s[i-1];
        GetString (i+1,j-1);
            cout<<s[i-1];//} else {if (dp[i][j]==dp[i+1][j]+1) {cout<<s[i-1];
            GetString (I+1,J);
        cout<<s[i-1];
            } else {cout<<s[j-1];
            GetString (i,j-1);
        cout<<s[j-1];
    }}} int main () {Ios::sync_with_stdio (false);
        while (cin>>s) {len=s.size (); 
                for (int l=2;l<=len;l++) {for (int i=0;i<=len-l+1;i++) {int j=i+l-1;
                if (S[i-1]==s[j-1]) dp[i][j]=dp[i+1][j-1];
                Else    Dp[i][j]=min (Dp[i][j-1],dp[i+1][j]) +1;
        }} cout<<dp[1][len]<< "";
        GetString (1,len);
    cout<<endl;
} return 0;

 }

Answer:
Using the idea of dynamic programming, first set DP[I][J] indicates the number of characters to be inserted into the Palindrome string to form the I-character to the J-character.
Next state transfer, if the I character and the first J character is the same, then dp[i][j]=dp[i+1][j-1], at this time do not need to insert any characters.
If the first character and the J character are not the same, then Dp[i][j]=min (Dp[i+1][j],dp[i][j-1]) +1, the meaning of the equation is compared to the first i+ 1 characters to the first J characters to form a palindrome string to be inserted between the minimum number of characters and the first character to the j-1 character to form a palindrome string need to insert the minimum number of characters, if DP[I+1][J] is less than dp[i][j-1], at this time you can insert a and s[i after the first J characters) The same characters are used to form palindrome strings, similarly. If DP[I][J-1] is less than or equal to dp[i+1][j], you can insert a string that is the same as s[j] in front of I.

Finally, note the print path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.