PalindromeTime limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 3265 Accepted Submission (s): 1130
Problem Descriptiona Palindrome is a symmetrical string, which is, a string read identically from the left to the right as well as From the right to the left. You-to-write a program which, given a string, determines the minimal number of characters to being inserted into the Stri Ng in order to obtain a palindrome.
As an example, by inserting 2 characters, the string "ab3bd" can is transformed into a palindrome ("Dab3bad" or "Adb3bda") . However, inserting fewer than 2 characters does not produce a palindrome.
Inputyour program was to read from standard input. The first line contains one integer:the length of the input string n, 3 <= n <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from "a" to "Z ', lowercase letters from" a "to" Z ' and digits from ' 0 "to ' 9 '. Uppercase and lowercase letters is to be considered distinct.
Outputyour program is-to-write to standard output. The first line contains one integer and which is the desired minimal number.
Sample Input
5ab3bd
Sample Output
2
Strategy: RT
Code:
#include <cstdio> #include <cstring> #include <algorithm>const int M = 5050;using namespace Std;int dp[2 ][m];char A[m], b[m];int n;int main () {while (scanf ("%d", &n) = = 1) { scanf ("%s", a); int I, J; for (i = n-1; I >= 0; I-) b[n-1-i] = A[i]; memset (DP, 0, sizeof (DP)); for (i = 1; I <= n, i + +) {for (j = 1; J <= N; j + +) { if (a[i-1] = b[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1] + 1;< C8/>else Dp[i%2][j] = max (dp[(i-1)%2][j], dp[i%2][j-1]); } } printf ("%d\n", N-dp[n%2][n]); } return 0;}
Hdoj 1513 palindrome "LCS" + "scrolling array"