Palindrome
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 55018 |
|
Accepted: 19024 |
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
Test instructionsgive you a string of length n, ask at least how many words rp compose a palindrome string;
"Analysis"
Original string: ab3bd
Flip Back string: Db3ba
There are repeating substring b3b, if you want to make a palindrome string, you must add other characters besides the repeating substring. such as:adb3bDA
The following problem is to find the original string and the longest common substring of the inverted string, that is, the problem of LCS;
"LCS issue"
The length of the longest common substring that marks the S1,S2 character position variable i,j, so dp[i][j] is the string s1[1~i],s2[1~j] , the state transition equation is as follows:
DP[I][J] = s1[i] = = S2[j]? DP[I-1][J-1]: Max (Dp[i-1][j], dp[i][j-1]);
Note
For the subject, the range of n is [3,5000], if the direct open 5000*5000 two-dimensional array memory overrun (of course, I heard that with short int AC will drift through);
"Scrolling array"
The function of a scrolling array is to optimize space. The main applications are in recursive or dynamic programming (e.g. 01 knapsack problems). Because DP topic is a bottom-up expansion process, we often need to use a continuous solution, the previous solution can often be shed. So using the scroll array optimization is very effective. The use of a scrolling array can be used to compress storage in large n situations.
For example, the value of dp[i][j] is only determined by dp[i-1][j-1], dp[i][j-1], dp[i-1][j], and, to be blunt, the state of the I can be calculated only by preserving the state of the i-1, so the DP can only open a 2*5000 array solution;
Maybe someone asked J why can't it also open to 2? This is good to say, because J is a continuous loop with I, I add a J all cycle once, so I in the constant change need to constantly j all the information, we can also make I with J constantly change, so just change into 5000*2, the other exactly the same;
Code
1 /*LCS*/2 3#include <iostream>4#include <cstdio>5#include <cstdlib>6#include <cstring>7 using namespacestd;8 Const intMAXN =5010;9 CharS1[MAXN], S2[MAXN];Ten intN; One intdp[3][MAXN]; A - voidLCS () - { theMemset (DP,0,sizeof(DP)); - - for(inti =1; I <= N; i++) - { + for(intj =1; J <= N; J + +) - { + //cout << s1[i] << "<< s2[j] << Endl; A if(S1[i] = =S2[j]) atdp[i%2][J] = dp[(i-1)%2][j-1]+1; - Else -dp[i%2][J] = max (dp[(i-1)%2][J], dp[i%2][j-1]); - } - } - //cout << dp[n%2][n] << Endl; inprintf"%d\n", n-dp[n%2][n]); - to + } - the intMain () * { $ while(~SCANF ("%d", &N))Panax Notoginseng { -scanf"%s", s1+1); the + for(inti =0; I < n; i++) As2[i+1] = s1[n-i]; the + LCS (); - $ } $ return 0; -}
View Code
POJ 1159-palindrome (LCS, scrolling array)