Poj 3280 Cheapest Palindrome (DP)
Question: There is a string consisting of n lower-case letters with a length of m. You can add or delete characters to it to convert it into a return text. Adding or deleting a character has a minimum cost.
Idea: DP. Dp [I] [j] indicates the str [I ~ J] The minimum cost for returning objects. Therefore, the state transition equation is:
When str [I] = str [j] dp [I] [j] = dp [I + 1] [J-1];
When str [I]! = Str [j] dp [I] [j] = min (dp [I + 1] [j] + add [str [I]-'a'], dp [I] [J-1] + add [str [j]-'a'],
Dp [I + 1] [j] + del [str [I]-'a'], dp [I] [J-1] + del [str [j]-'a']);
In fact, dp is hard to escape from three ideas:
1. One-dimensional linear dp: each time I is considered, the optimal sub-problem is selected either in the I-1 or in the 1... I-1;
2. Two-dimensional linear dp: when considering the (I, j) subproblem, select the optimal subproblem either in (I + 1, j), (I, J-1 ), either in I <= k <= j, or in k;
3. tree-like dp: When the I node is the best, the sub-node is the best. Generally, the Dual dp OF THE 01 backpack dp is integrated.
In the above 3 model, I found it only after doing the question.
In fact, this dp question can follow the idea in section 2nd.
Suppose a string Xx... yY; how can I find this string?
The situation is discussed in section 4:
1. Remove X and retrieve x... yY;
2. Remove Y and retrieve Xx... y;
3. Add X to the left and return the text of Xx... yYX;
4. Add Y to the right and return YXx... y.
As for the removal of X and Y, there is certainly no cost-effectiveness in the case of 1st and 2; plus X and Y, there will certainly be no cost-effectiveness in the case of 3rd and 4.
Therefore, the minimum cost of changing dp [I] [j] To I... j is the return string.
Equation:
Dp [I] [j] = min {Dp [I + 1] [j] + {cost of removing X}, dp [I + 1] [j] + {cost of adding X }, dp [I] [J-1] + {cost of removing Y}, dp [I] [J-1] + {cost of adding Y }};
In fact, the analysis shows that for X, you only need to remove and add the minimum cost of X (because the preceding dp string is the same), and Y is the same.
Therefore, the final result is as follows:
Dp [I] [j] = min {Dp [I + 1] [j] + min {cost of removing X}, {cost of adding X }}, dp [I] [J-1] + min {remove cost of Y}, {add cost of Y }}};
Some precautions for dp:
For example, when X and Y are the same, the minimum cost of x... y must be set in dp.
Source code: (14056 K, 79 MS) # include
# Define min (a, B) (a) <(B )? (A) :( B) using namespace std; const int MAX = 2005; int dp [MAX] [MAX]; int add [30], del [30]; int main () {int n, m, I, j, len; char c, str [MAX]; cin >>n> m >> str; for (I = 0; I <n; I ++) {cin> c; cin> add [c-'a']> del [c-'a'];} for (len = 1; len <m; len ++) for (I = 0; I + len <m; I ++) {j = I + len; if (str [I] = str [j]) {dp [I] [j] = dp [I + 1] [J-1];} else {dp [I] [j] = min (dp [I + 1] [j] + add [str [I]-'a'], dp [I] [J-1] + add [str [j]-'a']), min (dp [I + 1] [j] + del [str [I]-'a'], dp [I] [J-1] + del [str [j]-'a']); }}cout <dp [0] m-1] <endl; return 0 ;}