Question; uva11151longest palindrome (recurrence)
A string that can be obtained by removing some characters.
Solution: DP [I] [J] represents the maximum length of the return from string I bit to J bit.
If s [I] = s [J], DP [I] [J] = DP [I-1] [J-1] + 2, because the head and tail are the same, if the longest text return from I to J is required, only 2 is required for the longest text return in the middle.
Otherwise, DP [I] [J] = max (DP [I] [J-1], DP [I + 1] [J ]);
Computing sequence: fixed length is calculated each time, and the length ranges from small to large. Note: Empty strings.
Code:
#include <cstdio>#include <cstring>const int N = 1005;int dp[N][N];char str[N];void init (int len) {for (int i = 0; i < len; i++) {dp[i][i] = 1;if (i + 1 < len) {if (str[i] == str[i + 1])dp[i][i + 1] = 2;elsedp[i][i + 1] = 1;}}}int Max (const int a, const int b) { return a > b ? a: b; }int main () {int cas;int len;scanf ("%d%*c", &cas);while (cas--) {gets(str);len = strlen (str);if (!len) {printf ("0\n");continue;}init (len);for (int n = 3; n <= len; n++) {for (int i = 0, j = n - 1; j < len; i++, j++) {if (str[i] == str[j])dp[i][j] = dp[i + 1][j - 1] + 2;else dp[i][j] = Max (dp[i + 1][j], dp[i][j - 1]); }}printf ("%d\n", dp[0][len - 1]);}return 0;}