uva11404palindromic subsequence (max palindrome string, interval DP)
Description
A subsequence is a sequence obtained by deleting zero or more characters in a string. A palindrome is a string which if read from left to right, reads same as when read from right to left. Given a string, find the longest palindromic subsequence. If There is many answers to it, print the one of that comes lexicographically earliest.
Constraints
- Maximum length of string is 1000.
- Each string have characters 'a' to 'z' only.
Input
Input consists of several strings, each with a separate line. Input is terminated by EOF.
Output
For each line in the input, print the output of a single line.
Sample Input
Aabbaabbcomputerabzlasamhita
Sample Output
Aabbaacabaaha
Test instructions
Given a string s, the deletion of s, so that the remaining substring is a palindrome string, the output of the longest string, when there are more than the same length of the output dictionary order of the smallest.
Ideas:
Because the string is to be output, it is convenient to save the string in the state transfer process with C + + string, then the method of finding the longest palindrome is the same.
Defines the length of the structure, as well as the string, respectively, with Len, S to denote
The transition equation for the state is, if the end of the same, Dp[i][j].len = Dp[i + 1][j-1].len + 2 (length plus the end, so increase 2); If different, then the palindrome length does not increase Dp[i][j].len = max (dp[i + 1][j].len , Dp[i][j-1].len);
If the length is the same dp[i + 1][j].len, = = Dp[i][j-1].len, then we should compare the dictionary order of the substring, take the dictionary order small,
That is to judge Dp[i + 1][J].S, Dp[i][j-1].s.
<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < string> #include <algorithm> #include <queue> #include <stack>using namespace Std;const Double PI = ACOs ( -1.0); const double e = 2.718281828459;const double eps = 1e-8;const int maxn = 1010;struct str{int len; string S;} Dp[maxn][maxn];char S[maxn];int Main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); while (scanf ("%s", s+1)! = EOF) {int len = strlen (s+1); for (int i = 1; I <= len; i++) {//Initialize Dp[i][i].len = 1; DP[I][I].S = S[i]; } for (int k = 2; k <= len; k++)//control interval size {for (int i = 1, j = k; J <= Len; i++, J + +)//positive push for (int i = len-k+1, j = len; I >= 1; I--, j--)//Reverse Push {if (s[i] = = S[j]) {Dp[i][j].len = dp[i+1][j-1].len+2; DP[I][J].S = S[i]+dp[i+1][j-1].s+s[j]; } else {if (Dp[i][j-1].len > Dp[i+1][j].len | | (DP[I][J-1].LEN==DP[I+1][J].LEN&&DP[I][J-1].S<DP[I+1][J].S)) {//When [I, j-1] is longer than [I+1, j], or the length of the two is equal and//[i, the dictionary order of J-1] is less than [I+1, j] of the dictionary, select [I, J-1], otherwise select the latter Dp[i][j].len = Dp[i][j-1].len; DP[I][J].S = DP[I][J-1].S; } else {dp[i][j].len = Dp[i+1][j].len; DP[I][J].S = DP[I+1][J].S; }}}} cout<<dp[1][len].s<<endl; } return 0;} </span>
Uva11404palindromic subsequence (Max palindrome string, interval DP)