The main effect of the topic
Give a string, ask to add at least a character, turn it into a palindrome string, and output.
Ideas
A simple interval DP,
F (I, j) represents the minimum number of strings added in the interval (i, j), and becomes a palindrome string
So, if STR[I]==STR[J], F (i, j) = f (i+1, j-1) + 1
F (i, j) = Min{f (I+1, J), F (i, j-1)} + 1;
The topic wants to output the plan, then only then opens an array, according to the state transfer recursive output can
Code
Memory Search + recursive interval DP all have implementations
/**========================================== * is a solution to ACM/ICPC problem * * @source: uva-10453 make P Alindrome * @type: Memory Search, Interval DP * @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> using namespace std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
const int MAXN = 1010;
Char STR[MAXN];
int Len;
int F[MAXN][MAXN];
Memory search version, f array initialization INF int dfs (int i, int j) {if (I>j | | i==j) return F[I][J] = 0; if (F[i][j]!= inf) return F[I][J];
int& ans = f[i][j];
if (str[i] = = Str[j]) ans = DFS (i+1, j-1);
ans = min (ans, min (Dfs (i+1, j), Dfs (I, j-1)) +1);
return F[I][J]; }//recursive interval DP//www.bianceng.cn void dp () {memset (f, 0, sizeof (f)), for (int d = 2; d <= Len; ++d) for (int l = 0; L+d-1; = Len; ++L) {int r = l + d-1; int& ans = f[l][r] = INF; if (str[l] = = Str[r]) ans = f[l+1][r-1] ans = min (ans, min (f[l+1][
R], F[l][r-1]) +1); } void output (int i, int j) {if (i > J) return; if (i==j) {printf ('%c ', str[i]); return; if (str[i] = = Str[j ]) {printf ("%c", Str[i]), Output (i+1, j-1), printf ("%c", str[i);} else if (f[i][j] = = F[i+1][j] + 1) {printf ("%c", str
[i]);
Output (i+1, j);
printf ("%c", Str[i]);
else {printf ('%c ', str[j]); output (I, j-1); printf ("%c", Str[j]);}
int main () {while (gets (str)) {len = strlen (str); DP (); printf ("%d", f[0][len-1]);
Output (0, len-1);
Putchar (' \ n ');
return 0; }