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): 3276 Accepted Submission (s): 1134


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
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Cmath>using namespace Std;char Str[5010];char s[5010];int dp[2][5010];  DP[5010][5010], int main () {int n;    while (~SCANF ("%d", &n))    {         int i,j;        scanf ("%s", str);        for (i=0;i<n;i++)        {        //scanf ("%c", &str[i]);//output Limit exceeded        s[n-1-i]=str[i];        }    memset (DP, 0, sizeof (DP)),//DP, can optimize memory, apply a scrolling array for         (i = 1; I <= n; i + +) {for            (j = 1; J <= N; j + +) {                if (str[i-1] = = S[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1] + 1;//dp[i][j]=dp[i-1][j-i]+1;                 else dp[i%2][j] = max (dp[(i-1)%2][j], dp[i%2][j-1]);  DP[I][J] = max (Dp[i-1][j], dp[i][j-1]            }        }        printf ("%d\n", N-dp[n%2][n]);  N-dp[n][n]     
<span style= "font-family:arial; font-size:14.44444465637207px; line-height:25.98958396911621px; " > scrolling array is actually a space-saving approach, there is no advantage in time, more for DP, for example:  </span><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:arial; font-size:14.44444465637207px; line-height:25.98958396911621px; " ></p><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:arial; font-size:14.44444465637207px; line-height:25.98958396911621px; " ></p><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:arial; font-size:14.44444465637207px; line-height:25.98958396911621px; " > A DP, usually if need 1000x1000 space, in fact, according to DP <strong> no effect </strong>, can be opened into 2x1000, and then by scrolling, to achieve and 1000x1000 the same effect. Scrolling array is commonly used in DP, in the DP process, when we move from one state to another state, it is possible that some of the state information stored previously is useless, for example, in the 01 knapsack problem, we should open the two-dimensional array of dp[i][j] from the understanding angle, the first dimension we deposited to the number of items, This is the stage, the second-dimensional storage capacity, but we get dp[i], just use dp[i-1] information, dp[i-k],k>1 are useless space, so weThe array can be opened into one dimension on the line, the iteration update the contents of the array, scrolling array is the same principle, the purpose is the same, but this time the problem is often impossible to shrink into one dimension, such as a dp[i][j] need to dp[i-1][k],dp[i-2][k] decision, i<n,0 <k<=10;n <= 100000000; Obviously not a one-dimensional, normal we should open a dp[100000005][11] array, the result is obvious, ultra-memory, in fact, we just open dp[3][11] is enough dp[i%3][j] by dp[ (i-1)%3][k] and dp[(i-2)%3][k] decided that space complexity varied greatly. </p><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:arial; font-size:14.44444465637207px; line-height:25.98958396911621px; " ><span style= "Background-color:rgb (245, 245, 245); Color:rgb (51, 51, 51); Font-family:helvetica, Tahoma, Arial, Sans-serif; line-height:24px; " ><strong> no effect </strong> that is, the current state is a complete summary of history, and how to achieve this state is irrelevant. </span></p>

For example, for this word solitaire, each word is used up to two times.
then "The word currently received" can not summarize the whole "history", because it is also received this word, the previously considered words are used
No, how many times it will affect the future development, and the single state parameter cannot generalize the information. If this information is added to the State
parameters, the state is too many (the number of points), dynamic planning is not much significance.

If there is not much information affecting history, we can use the ascending dimension method to make our state have no effect,
So when we are thinking about the state, the guiding ideology is "concise and complete summary of history"


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.