HDU 1513 palindrome "LCS"

Source: Internet
Author: User

Test instructions: Give a string s, ask at least how many letters to make it into a palindrome

Solution: Find the length of the longest common subsequence after the string is flipped, and subtract the length of the longest common subsequence by the length of the string.

Reflection: Because the title gives the range of n is 3<=n<=5000, so the dp[][] array if open to dp[5005][5005], will be super memory, this time should be used to optimize the scrolling array

Detailed introduction to scrolling arrays http://blog.csdn.net/niushuai666/article/details/6677982

Palindrome

Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 3301 Accepted Submission (s): 1140

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, is-to-write-to-standard output. The first line contains one integer, which is the desired minimal number. sample input5ab3bd sample output2&nbsp ;
#include <stdio.h> #include <string.h>char s[5005],w[5005];int dp[2][5005];int max (int a,int b) {if (a>b) return A;elsereturn B;} int main () {int n,i,j,x,y;while (scanf ("%d", &n) ==1) {scanf ("%s", &s); for (i=0;i<n;i++) w[i]=s[n-1-i];w[i]= ' Memset (Dp,0,sizeof (DP)); for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {   x=i%2;   Y=1-x;  if (S[i-1]==w[j-1])  dp[x][j]=dp[y][j-1]+1;  else  Dp[x][j]=max (dp[y][j],dp[x][j-1]);}    } printf ("%d\n", N-dp[n%2][n]);}}

  

HDU 1513 palindrome "LCS"

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.