Hdoj 1513 palindrome "LCS" + "scrolling array"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.