UVA 11151 Longest palindrome

Source: Internet
Author: User

Original question:
A palindrome is a string, reads the same from the left as it does from the right. For example, I, GAG and MADAM am palindromes,but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-palindromic string, you can always take away some letters, and get a palindromic subsequence. For example, given the string ADAM, you remove the letter M and get
A palindrome ADA. Write a program to determine the length of the longest palindrome you can get from a string.
Input
The first line of input contains an integer T (≤60). Each of the next T lines is a string, whose length
is than 1000.
for≥90% of the test cases, string length≤255.
Output
For each input string, your program should print the length of the longest palindrome you can get by
Removing zero or more characters from it.
Sample Input
2
ADAM
MADAM
Sample Output
3
5
Effect:
Give you a string, you can delete 0 or more characters in this string to make this string a palindrome, now let you find the longest can become a palindrome.

#include <bits/stdc++.h> using namespace std;
FStream in,out;
int dp[1001][1001];
Char s[1001];
    int max3 (int x,int y,int z) {return max (max (x, y), Max (Y,z)),} int main () {//Ios::sync_with_stdio (FALSE);
    int N,ans;
    scanf ("%d", &n);
    GetChar ();
        while (n--) {gets (s);
        Memset (Dp,0,sizeof (DP));
        int Len=strlen (s);
            if (len==0) {printf ("0\n");
        Continue
        } for (int i=0;i<len;i++) dp[i][i]=1;
            for (int i=0;i<len-1;i++) if (s[i]==s[i+1]) dp[i+1][i]=dp[i][i+1]=2;
        else dp[i+1][i]=dp[i][i+1]=1; 
                for (int l=3;l<=len;l++) {for (int i=0;i<=len-l+1;i++) {int j=l+i-1;
                if (S[i]==s[j]) Dp[i][j]=max (Dp[i+1][j-1]+2,dp[i][j]);
            DP[I][J]=MAX3 (Dp[i+1][j],dp[i][j-1],dp[i][j]); }} PrintF ("%d\n", dp[0][len-1]);
} return 0;

 }

Answer:
There have been a lot of dynamic programming questions about palindrome, and the transfer equation almost never comes out. Using the idea of interval dynamic programming to set the state Dp[i][j] to the letter I to the letter J character can form the longest palindrome, consider the state transfer, if S[I]==S[J] so dp[i][j]=dp[i+1][j-1]+ 2, here to initialize the time to note that two two consecutive identical letters to be written in advance. If S[i]!=s[j],dp[i][j]=max (Dp[i+1][j],dp[i][j-1])
The problem can also be done with the longest common subsequence, which is to let the string itself and this string to reverse the problem of LCS.

Note that there are empty strings ... Also pay attention to the carriage return problem

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.