[Cpp]
Description: converts a string to a text return by adding a string, and then outputs the text return.
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
Char str [1010];
Int p [1010] [1010];
Int v [1, 1010] [2, 1010];
Int min (int x, int y)
{
Return x> y? Y: x;
}
Int dp (int x, int y)
{
If (v [x] [y]! =-1) return v [x] [y];
If (x> = y) return v [x] [y] = 0;
If (str [x] = str [y])
{
P [x] [y] = 1;
V [x] [y] = dp (x + 1, Y-1 );
}
Else
{
Int a = dp (x + 1, y );
Int B = dp (x, Y-1 );
If (a> B) p [x] [y] = 2;
Else p [x] [y] = 3;
V [x] [y] = min (a, B) + 1;
}
Return v [x] [y];
}
Void show (int x, int y)
{
If (x> y) return;
If (x = y) printf ("% c", str [x]);
If (p [x] [y] = 1)
{
Printf ("% c", str [x]);
Show (x + 1, Y-1 );
Printf ("% c", str [y]);
}
Else if (p [x] [y] = 2)
{
Printf ("% c", str [y]);
Show (x, Y-1 );
Printf ("% c", str [y]);
}
Else if (p [x] [y] = 3)
{
Printf ("% c", str [x]);
Show (x + 1, y );
Printf ("% c", str [x]);
}
}
Int main ()
{
// Freopen ("a.txt", "r", stdin );
While (gets (str ))
{
Int len = strlen (str );
Memset (v,-1, sizeof (v ));
Memset (p, 0, sizeof (p ));
Printf ("% d", dp (0, len-1 ));
Show (0, len-1 );
Puts ("");
}
Return 0;
}