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