Problem:
The longest palindrome subsequence of the given input string (the subsequence does not require continuous).
LPs (I,J) is used to denote the length of the longest palindrome sequence from the string I character to the J character, the length of the string is N, and LPs (1,n) is required:
LPS (I,J) = 0; i>j;
LPS (I,J) = 1; I==j;
LPS (i,j) =lps (i+1,j-1) +2; STR[I]==STR[J];
LPs (I,J) =max (LPs (I+1,J), LPs (i,j-1)); STR[I]!=STR[J];
Zuichanghuiwen2.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <string>using namespace Std;int c[20][20];void LPS ( String str) {int m = str.length (); int j;for (int j = 0; J < m; J + +) for (int i = j + 1, i < m; i++) c[i][j] = 0;for (in t i = 0; I < m; i++) C[i][i] = 1;for (int l=2;l<=m;l++) //This must be calculated by length from short start for (int i = 0; i<m-l+1; i++) {j = i + l-1;if (str[i] = = S TR[J]) C[i][j] = C[i + 1][j-1] + 2;elsec[i][j] = c[i + 1][j] > c[i][j-1]? C[i + 1][j]: c[i][j-1];}} void rebuild (String str) {int m = c[0][str.length ()-1];int mi;for (Auto i = 0; i < str.length (); i++) if (str[i] = = str [i + m-1]) {mi = i;break;} for (int i = mi; i < Mi + m; i++) cout << str[i];} int main () {string str ("DGDFGDFABCACBAGFGFG"); LPS (str); rebuild (str); while (1); return 0;}
The longest palindrome algorithm 2