POJ 1159-Palindrome (DP/LCS deformation)

Source: Internet
Author: User
Tags cmath

POJ 1159-Palindrome (DP/LCS deformation)
Palindrome

Time Limit:3000 MS Memory Limit:65536 K
Total Submissions:53770 Accepted:18570

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
Question: How many characters must a string be added to a return string (can be added anywhere)
Train of Thought: reverse the string s to get s' (note that strrev () Will CE sad), and then find the longest common subsequence (LCS) n-LCS of s and S' as the answer.
Surprised? In fact, because the longest public sub-sequences of s and s are definitely the same, the remaining length only needs to be added with n-LCS characters, which can meet the requirements of the overall retrieval .. It may be hard to understand how to add it. In this case, the strings in LCS are already in the background, so we don't need to worry about it. Then we just leave the remaining characters empty, for example, if the reverse expression of Ab3bd leads to s 'db3ba so LCS = 3 (b3b), then the remaining two characters are A and d. Now we insert them into the original string s in sequence: because A is on the leftmost side, insert A ab3rd digit to the rightmost side, and then the second digit to the right, so we can insert a d in the second place on the left to achieve adb31_ OK...
For LCS .. Struct will only be o (n * n) and then the array must be short ..
#include #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            #include 
             
              #define ll long long#define maxn 360#define pp pair
              
               #define INF 0x3f3f3f3f#define max(x,y) ( ((x) > (y)) ? (x) : (y) )#define min(x,y) ( ((x) > (y)) ? (y) : (x) )using namespace std;int n;short dp[5005][5005]={0};char s[5010],t[5010];void solve(){for(int i=0;i
               
              
             
           
          
         
        
       
      
     
    
   
  

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.