The main effect of the topic
Gives a string that outputs its longest palindrome string, and if there are multiple results, the output dictionary order is minimal.
Ideas
We all know that after a string is in reverse order and the original string into the longest common subsequence, you can calculate its longest palindrome string length.
But this problem is not only to output palindrome string, but also requires the smallest dictionary order, so it is very difficult to get.
Set STR1 is a positive sequence string, STR2 is a string after the reverse order
F[i][j].len represents the first I-bit of the STR1, the first J-bit of the str2, the length of the longest common substring
F[i] [J].str represents the first I-bit of str1, the first J-bit of str2, the smallest dictionary-order string of the longest common substring
State transitions are similar to normal LCS, except that the string with the smallest record dictionary order is added
But the final f[i][j].str is not necessarily the answer, because the longest common subsequence that is calculated is not a palindrome string.
For example:
Kfclbckibbibjccbej
Jebccjbibbikcblcfk
BCIBBIBC is their LCS, but it's not a palindrome.
But its front LEN/2 must be the first half of a palindrome string.
Know the former LEN/2, you can directly construct the second half of the palindrome string
Attention should be paid to the problem of the parity of length
Code
/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-11404 palind Romic subsequence * @type: LCS minimum Dictionary sequence palindrome * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuangde@gmail . com *===========================================*/#include <iostream> #include <cstdio> #include < algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring> #include
<cstdlib> using namespace std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
const int MAXN = 1010;
Char STR1[MAXN], STR2[MAXN];
int n, Len; struct node{int len; string str;}
F[MAXN][MAXN];
int main () {Node A, B;
while (gets (str1+1)) {//Reverse len = strlen (str1+1), for (int i=1; i<=len; ++i) str2[i] = Str1[len+1-i];
init for (int i=0; i<=len; ++i) F[0][i].len = 0, f[0][i].str = ""; LCS for (int i = 1; I <= len ++i) {for (int j = 1; j <= Len; ++J) {if (str1[i] = = Str2[j]) {F[i][j].len = F[i-1][j-1].len + 1; f[i][j].str = F[i-1][j-1].str + str1[i];
else {if (F[i-1][j].len > F[i][j-1].len) {f[i][j].len = F[i-1][j].len; f[i][j].str = f[i-1][j].str;
else if (F[i][j-1].len > F[i-1][j].len) {f[i][j].len = F[i][j-1].len; f[i][j].str = f[i][j-1].str;
else {f[i][j].len = F[i-1][j].len; f[i][j].str = min (f[i-1][j].str, f[i][j-1].str);}
an int maxx = F[len][len].len;
string ans = f[len][len].str; Output//www.bianceng.cn if (Maxx & 1) {for (int i = 0; i < MAXX/2, ++i) cout << ans[i]; for (int i = maxx/ 2; I >= 0;
(i) cout << ans[i];
Putchar (' \ n '); else {for (int i = 0; i < MAXX/2 ++i) cout << ans[i]; for (int i = maxx/2-1; I >= 0;-i) cout << ans[
I];
Putchar (' \ n ');
} return 0; }