Poj1159 Palindrome (longest common subsequence)
Palindrome
| Time Limit:3000 MS |
|
Memory Limit:65536 K |
| Total Submissions:52966 |
|
Accepted:18271 |
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
| 13368265 |
Happystick |
1159 |
Accepted |
148 K |
719 MS |
C ++ |
720B |
2014-08-23 21:42:27 |
// Minimum number of characters inserted = String Length-Maximum length of common subsequences in positive and backward order/* One-dimensional array + short optimized wa several times, when I was speechless, I started to think of multiple groups of test data. wa had been reading the code for half a day before I knew it .... Just a set of test data, but this code should be the best code. Refer to Nanyang excellent code. Similarly, the Code submitted in hdoj wa speechless Time: */# include
# Include
Int main () {char s1 [5005], s2 [5005]; short dp [5005]; int N; scanf ("% d", & N ); scanf ("% s", s1); for (int I = 0; I
Dp [J-1]? Temp: dp [J-1]; // compare} old = temp in dp [I-1] [j] and dp [I] [J-1; // The dp [I-1] [j] in the previous column is the dp [I-1] [J-1]} printf ("% d \ n ", n-dp [N-1]); return 0 ;}