Palindrome (Application of completion of retrieval string + Longest Common subsequence) hdu1513 + poj1159 + Dynamic Planning

Source: Internet
Author: User

Palindrome (Application of completion of retrieval string + Longest Common subsequence) hdu1513 + poj1159 + Dynamic Planning

 

Palindrome Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4277 Accepted Submission (s): 1462



Problem 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 adb3133). 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

Source IOI 2000

 

 

Question: A string is provided. It must contain at least a few characters to convert the string to a text string.
Idea: first obtain the longest common subsequence of the positive and negative strings, and then add the remaining two sides to the list. (Centered on the longest common subsequence)

Here, pay special attention to the need to scroll the array for more than 1000, otherwise it will exceed the memory.

 

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; int map [2] [5005]; // use a rolling array string str;/*** meaning: A string is given, to convert the string into a text string, you need to add at least a few characters. First, find the longest common subsequence of the positive and inverse strings, and then add the remaining two sides to the string. (Centered on the longest common subsequence) */void LCS (int & len1, int & len2) {for (int I = 0; I <= len1; I ++) {for (int j = 0; j <= len2; j ++) {if (I = 0 | j = 0) {map [I % 2] [j] = 0; continue;} if (str [I-1] = str [len2-j]) {map [I % 2] [j] = map [(I-1) % 2] [J-1] + 1;} else if (map [(I-1) % 2] [j]> = map [I % 2] [J-1]) {map [I % 2] [j] = map [(I-1) % 2] [j];} else {map [I % 2] [j] = map [I % 2] [J-1] ;}}} int main () {int n; while (cin> n) {cin> str; LCS (n, n); int ans = map [n % 2] [n]; cout <
    
     

 

 

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.