Poj1159--palindrome (DP: Longest common Subsequence variant + scrolling array)

Source: Internet
Author: User

Palindrome
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 53414 Accepted: 18449

Description

A palindrome is a symmetrical string, which is, a string read identically from the left to the right as well as from the right to left. You-to-write a program which, given a string, determines the minimal number of characters to being inserted into the STR ing 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. 

Input

Your 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.

Output

Your 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

Source

IOI 2000 topic: Adding the fewest characters to make a palindrome string if a string is a palindrome, then it and its inverse array of the longest common sub-sequence of its own length, so that the longest common sub-sequence of the length of the total length minus it, the result is to modify the length.

Array of 5000*5000 using short

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;short dp[5100][5100]; Char str1[5100], str2[5100]; int main () {    int i, j, N;    while (scanf ("%d", &n)!=eof)    {        scanf ("%s", str1);        for (i = 0; i < n; i++)            str2[n-1-i] = str1[i];        Str2[i] = ' + ';        for (i = 1; I <= N, i++) for            (j = 1; J <= N; j + +)            {                if (str1[i-1] = str2[j-1])                    dp[i][j] = dp[i -1][j-1]+1;                else                    dp[i][j] = max (dp[i-1][j],dp[i][j-1]);            }        printf ("%d\n", N-dp[n][n]);    }    return 0;}


Using a scrolling array

Scrolling arrays: Using two rows of arrays to simulate large two-dimensional arrays

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int dp[2][5100]; char STR1[5100], str2[5100]; int main () {    int i, J, N, K;    while (scanf ("%d", &n)!=eof)    {        scanf ("%s", str1);        for (i = 0; i < n; i++)            str2[n-1-i] = str1[i];        Str2[i] = ' + ';        k = 0;        for (i = 1; I <= n; i++)        {            k = 1-k;            for (j = 1; J <= N; j + +)            {                if (str1[i-1] = = str2[j-1])                    dp[k][j] = dp[1-k][j-1]+1;                else                    dp[k][j] = max (dp[1-k][j],dp[k][j-1]);            }        }        printf ("%d\n", N-dp[k][n]);    }    return 0;}


Poj1159--palindrome (DP: Longest common Subsequence variant + 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.