Poj1159 Palindrome (longest common subsequence)

Source: Internet
Author: User

Poj1159 Palindrome (longest common subsequence)
Palindrome

Time Limit:3000 MS Memory Limit:65536 K
Total Submissions:52966 Accepted:18271

Description

A palindrome is a regular rical string, that is, a string read identically from left to right as well as from right to left. you are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "adb3me "). however, inserting fewer than 2 characters does not produce a palindrome.

Input

Your program is 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 are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5Ab3bd

Sample Output

2
 
 
13368265 Happystick 1159 Accepted 148 K 719 MS C ++ 720B 2014-08-23 21:42:27
// Minimum number of characters inserted = String Length-Maximum length of common subsequences in positive and backward order/* One-dimensional array + short optimized wa several times, when I was speechless, I started to think of multiple groups of test data. wa had been reading the code for half a day before I knew it .... Just a set of test data, but this code should be the best code. Refer to Nanyang excellent code. Similarly, the Code submitted in hdoj wa speechless Time: */# include
  
   
# Include
   
    
Int main () {char s1 [5005], s2 [5005]; short dp [5005]; int N; scanf ("% d", & N ); scanf ("% s", s1); for (int I = 0; I
    
     
Dp [J-1]? Temp: dp [J-1]; // compare} old = temp in dp [I-1] [j] and dp [I] [J-1; // The dp [I-1] [j] in the previous column is the dp [I-1] [J-1]} printf ("% d \ n ", n-dp [N-1]); return 0 ;}
    
   
  

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.