POJ 1159 Palindrome

Source: Internet
Author: User

Palindrome
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 59094 Accepted: 20528

Description

A palindrome is a symmetrical string, which is, a string read identically from the left to the right as well as from the right to left. You-to-write a program which, given a string, determines the minimal number of characters to being inserted into the STR ing 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.

Input

Your 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.

Output

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

Sample Input

5ab3bd

Sample Output

2

Source

IOI 2000 Test Instructions:
Give a string of characters, and ask at least how many characters you can make it into a palindrome string.
(Palindrome string = Look from left to right and from right to left see the same string) the short array is directly over
/*F[i][j] Indicates that the minimum number of steps required to change from a character designator i-j to a palindrome is not difficult to draw if S[J]==S[I+J]: f[j][i+j]=f[j+1][i+j-1], otherwise: f[j][i+j]=min (f[j+1][i+j],f[j][i +J-1]) +1*/#include<cstdio>#include<iostream>using namespacestd;#defineN 5010CharS[n];intN; Short intF[n][n];intMain () {scanf ("%d%s",&n,s);  for(intI=1; i<=n;i++) {F[i][i]=0; f[i][i+1]=s[i]==s[i+1]?0:1;//Initialize    }     for(intI=1; i<=n;i++) {//Cycle Two-letter intervals         for(intj=0; i+j<n;j++) {//Circular Start Letter            if(S[j]==s[i+j]) f[j][i+j]=f[j+1][i+j-1]; ElseF[j][i+j]=min (f[j+1][i+j],f[j][i+j-1])+1; }} printf ("%d\n", f[0][n-1]); return 0;}

POJ 1159 Palindrome

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.