HDU 1513 (scrolling array)

Source: Internet
Author: User

Palindrome

Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4751 Accepted Submission (s): 1625


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 INPUT5AB3BD

Sample Output2

Source IOI 2000 This topic uses the interval DP to burst the memory. The correct solution is to use the LCS solution, and the LCS algorithm is only related to the current line and the previous row, so we use the rolling array to compress the space
///Test Instructions: Converts a string into a palindrome minimum number of characters to add///Solution: The string is reversed. The LCS of the original string and the current string are obtained, and the length of the LCS is reduced by the original strings to the minimum added characters///The benefit of this solution is that the DP array can be turned into a scrolling array#include <stdio.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;Const intN =5001;intdp[2][n];CharStr[n],str1[n];intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {scanf ("%s", str+1);  for(intI=1; i<=n;i++) {Str1[n-i+1]=Str[i]; }        ///printf ("%s", str1+1);Memset (DP,0,sizeof(DP));  for(intI=1; i<=n;i++){             for(intj=1; j<=n;j++){                if(str[i]==Str1[j]) {Dp[i%2][J] = dp[(i-1)%2][j-1]+1; }Else{dp[i%2][J] = max (dp[i%2][j-1],dp[(I-1)%2][j]); }}} printf ("%d\n", N-max (dp[1][n],dp[0][n])); }}

Put a burst of memory code: Open short is useless, HDU data strong ORZ ~ ~ ~

///Test Instructions: Converts a string into a palindrome minimum number of characters to add///solution: Interval Dp,dp[i][j] represents the interval i-j the number of characters to be added in order to turn it into a palindrome///if str[i] = = Str[j] then dp[i][j] = dp[i+1][j-1]///otherwise dp[i][j] = min (Dp[i+1][j], dp[i][j-1]) +1 is considered to be added before the first i+1 character Str[j] or in///add a character after j-1 Str[i]#include <stdio.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;Const intN = the; ShortDp[n][n];CharStr[n];intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {scanf ("%s", str);  for(intI=1; i<=n;i++) dp[i][i]=0;///the interval length of 1 does not need to be added is a palindrome string         for(intL =2; l<=n;l++){             for(intI=0; i<=n-l+1; i++){                intj = i+l-1; if(str[i]==Str[j]) {Dp[i][j]= dp[i+1][j-1]; }Else{Dp[i][j]= Min (dp[i+1][j],dp[i][j-1])+1; }}} printf ("%d\n", dp[0][n-1]); }}

HDU 1513 (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.