1159 -- Palindrome (dp: text string deformation 2), 1159 -- palindromedp
Palindrome
Time Limit:3000 MS |
|
Memory Limit:65536 K |
Total Submissions:53431 |
|
Accepted:18454 |
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
Source
IOI 2000, another dp Method, dp [l] [I], indicates the number of operations required for a string whose length is l starting with the I character. For a string with a length of 1, the operation is a string with a length of 0 and 2. If the two characters are the same, the operation is 0. For different operations, the operation is a string with a length of 1 and 3. If it is the same between the left and right, the operation is 0, which is different between the left and right. For a [1], a [2], a [3], you can add a [3] or a [1] Later. then, you only need to determine the operations required for the remaining two and characters. A 4-character string with the same length between the left and right must contain two characters in the middle. If it is different, it must be determined in the same way as if it is 3 characters in length. The string with the length starting from l to I can be obtained from L-1 as I, the length starting from l-1 as I + 1, or the length as L-2, start with I + 1. Dp Formula
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp[3][5100] ;char str[5100] ;int main(){ int i , l , k1 , k2 , k3 , n ; while(scanf("%d", &n) !=EOF) { scanf("%s", str); for(i = n ; i >= 0 ; i--) str[i] = str[i-1] ; k1 = -1 ; k2 = 0 ; k3 = 1; memset(dp,0,sizeof(dp)); for(l = 2 ; l <= n ; l++) { k3++ ; if(k3 == 3) k3 = 0 ; if(k3 == 0){ k2 = 2 ; k1 = 1 ; } else if( k3 == 1 ){ k2 = 0 ; k1 = 2 ; } else { k2 = 1 ; k1 = 0 ; } for(i = 1 ; i <= n-l+1 ; i++) { if( str[i] == str[i+l-1] ) dp[k3][i] = min( min(dp[k2][i]+1,dp[k2][i+1]+1),dp[k1][i+1] ) ; else dp[k3][i] = min( dp[k2][i]+1 , dp[k2][i+1]+1); } } printf("%d\n", dp[k3][1]); } return 0;}
1. Fill in the blanks in the Code, such as "abccba" and "abcba". The following code checks whether a string is blank.
Char buf [] = "abcde11edcba ";
Int x = 1;
For (int I = 0; I <strlen (buf)-1-I; I ++)
{
If (buf [I]! = Buf [strlen (buf)-1-I]) {printf ("not a return string"); exit (0 );}
}
Printf ("A text string ");
I haven't written C language for a long time, and the code may be wrong. I hope the train of thought can be used as a reference.
2. (40 points) The function of the program below can be: judge whether a string is a return string (the return string refers to a string with the same positive read and reverse read, for example: String
[1]
# Include "string. h"
# Include "stdio. h"
Void main ()
{
Char x [20];
Int I, j, n;
Gets (x );
N = strlen (x );
I = 0;
J = n-1;
While (I <j)
{
If (x [I]! = X [j]) break;
I ++;
J --;
}
If (I> = j)
Printf ("The number is palindrome ");
Else printf ("The number is not palindrome ");
}
[2]
# Include <stdio. h>
Void main ()
{
Int I, a [10];
Printf ("input 10 numbers: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", a [I]);
Printf ("\ n ");
For (I = 0; I <= 9; I ++)
Printf ("% d", a [I]);
Printf ("\ n ");
For (I = 9; I <= 0; I --)
Printf ("% d", a [I]);
}