HDU 1513 oldest sequence

Source: Internet
Author: User

Palindrome

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


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

Sourceioi 2000 title meaning: to a string of length n, ask at least how many characters are added to make the string a palindrome. Idea: Turn the original string upside down, two strings to the eldest son sequence m, then N-m is the number of characters added. The longest subsequence length means the fewest number of characters added. Because N is too large, you need to use a scrolling array.  DP[I&AMP;1][J] means dp[i][j]; dp[! ( i&1)][j] = dp[i-1][j] Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <vector>6#include <queue>7#include <cmath>8#include <Set>9 using namespacestd;Ten  One #defineN 5005 A  - intMaxintXintY) {returnX>y?x:y;} - intMinintXintY) {returnX<y?x:y;} the intAbsintXintY) {returnx<0?-x:x;} -  -  - intdp[2][n]; + intN; - CharS[n]; +  A Main () at { -     intI, J, K; -      while(SCANF ("%d", &n) = =1){ -Memset (DP,0,sizeof(DP)); -scanf"%s", s+1); -         CharS1[n]; in          for(i=n;i>=1; i--) s1[i]=s[n-i+1]; -         intMaxh=0; to          for(i=1; i<=n;i++){ +memset (dp[i&1],0,sizeof(dp[i&1])); -              for(j=1; j<=n;j++){ the                 if(s[i]==S1[j]) { *dp[i&1][j]=max (dp[! ( i&1)][j-1]+1,dp[i&1][j]); $                 }Panax Notoginseng                 Elsedp[i&1][j]=max (dp[! ( i&1)][j],dp[i&1][j-1]); -Maxh=max (maxh,dp[i&1][j]); the             } +         } Aprintf"%d\n", N-maxh); the     } +}

HDU 1513 oldest sequence

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.