1 (longest public substrings (note the difference between the longest public substrings ))
The two strings str1 and str2 have the following lengths: (L1, L2)
DP [I] [J] represents the length of the common subsequence that can be reached by two strings ending with the I and J, respectively, because the following involves I-1 and J-1, in this case, we generally start from I = 1 and j = 1 to I <= len1,
J <= len2.
If (STR [I-1] = STR [J-1])
DP [I] [J] = DP [I-1] [J-1] + 1;
If (STR [I-1]! = STR [J-1])
DP [I] [J] = 0;
0; I = 0 or J = 0;
There is dp = DP [I] [J] = DP [I-1] [J-1] + 1; I> 0 and j> 0 and listen [I-1] = CH2 [J-1];
DP [I] [J] = 0; I> 0 and j> 0 and then [I-1]! = CH2 [J-1];
Note that DP [I] [0] = 0 (0 <= I <= max (L1, L2 ); DP [0] [I] = 0 (0 <= I <= max (L1, L2 );
The longest Public String must be consecutive among the original characters, and the longest public subsequence is not required.
Longest Common subsequence:
According to the nature of the longest common subsequence problem, we can specify that DP [I] [J] is the length of the longest common subsequence of the first I character of string 1 and the first J character of string 2, since I-1 and J-1 are involved below, we generally start from I = 1 and j = 1 to I <= len1,
J <= len2.
1ch1 [I-1] = CH2 [J-1], then DP [I] [J] = DP [I-1] [J-1] + 1;
2 Tib [I-1]! = CH2 [J-1], then we know that substring [I] and CH2 [J] cannot appear in the same common subsequence, in this case, the longest subsequence may end with limit [I] Or CH2 [J], because DP [I] [J] =
Max {DP [I-1] [J], DP [I] [J-1]};
In this case, all DP [I] [J] = 0 for I = 0 or J = 0;
0; I = 0 or J = 0;
There is dp = DP [I] [J] = DP [I-1] [J-1] + 1; I> 0 and j> 0 and listen [I-1] = CH2 [J-1];
DP [I] [J] = max {DP [I-1] [J], DP [I] [J-1]}; I> 0 and j> 0 and then [I-1]! = CH2 [J-1];
2 (longest ascending or descending subsequence)
Given a sequence A1, a2......;
DP [I] indicates the longest ascending sub-sequence length ending with AI (descending opposite)
Core code:
For (I = 1; I <= N; I ++ ){
DP [I] = 1;
For (k = 1; k <I; k ++ ){
If (AK <AI & DP [I] <DP [k] + 1)
DP [I] = DP [k] + 1;
}
}
Note that the sub-sequence cannot be increased or decreased at most.
3 (greatest subsequence and problem)
Given a sequence A1, a2......;
The sum of subsequences and the largest problem DP [I] indicates the subsequences ending with AI and Max indicates the largest subsequences and
Core:
1. If all input data is negative, the maximum value is the maximum value in the sequence.
2. If there is a positive number
For (I = 1; I <= N; I ++ ){
DP [I] = DP [I-1] + AI;
If (DP [I] <0)
DP [I] = 0;
If (max <DP [I])
Max = DP [I];
}
4 (Data tower problem)
Given an array s [N] [m] to form a data Tower, find the path from the top to the bottom and the maximum
I used the bottom-up approach to solve the problem (note that it starts from the last row)
DP [I] [J] indicates that the maximum value of Row J is reached.
Then there is DP [I] [J] = max {DP [I-1] [J-1], DP [I-1] [J]} + s [I] [J];
For (I = n-1; I> = 1; I --){
For (j = 1; j <= I; j ++ ){
DP [I] [J] = max {DP [I-1] [J-1], DP [I-1] [J]} + s [I] [J]
}
}
The last DP [1] [1] is the maximum value.
5 (01 backpack problems)
There are n items and a backpack with a capacity of v. The volume of the I-th item is V [I], and the value is C [I]. Solving which items are loaded into a backpack can maximize the total value.
We know that we can put or not put a single item.
DP [I] [J] indicates the maximum value of the I-th item obtained from a backpack with a size of J.
DP [I] [J] = max {DP [I-1] [J-V [I] + C [I], DP [I-1] [J]};
Here, if we push back from J = V, we can optimize it
DP [J] = max {DP [J], DP [J-V [I] + C [I]};
Core code:
For (I = 1; I <= N; I ++ ){
For (j = V; j> = 0; j --){
If (j> = V [I])
DP [J] = max {DP [J], DP [J-V [I] + C [I]};
}
}
DP [v] is the greatest value
6 (full backpack problem)
There are n items and a backpack with a capacity of V, each of which has unlimited items available. The volume of item I is V [I], and the value is C [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.
At this time, it is not a problem to put or not to put items, but to put 0 pieces of 1 piece .......
At this time, we can be like a 01 backpack.
DP [I] [J] indicates whether the I-th item of the backpack with a size of J needs to be placed again. Therefore, we need to go through a sequential loop from 0 to v.
DP [I] [J] = max {DP [I-1] [J-V [I] + C [I], DP [I-1] [J]} (note that this is the same as 01 backpack but the process of solving it is different)
After optimization: DP [J] = max {DP [J], DP [J-V [I] + C [I]};
Core code:
For (I = 1; I <= N; I ++ ){
For (j = V [I]; j <= V; j ++) {// note that this is from V [I] to V
If (j> = V [I])
DP [J] = max {DP [J], DP [J-V [I] + C [I]};
}
}
Note that the DP [v] obtained in this column is the largest because it is always superimposed.
7 (multiple knapsack problems)
There are n items and a backpack with a capacity of v. A maximum of N [I] items are available for the I-th item. The cost per item is C [I] and the value is W [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.
Because there are N [I] + 1 policies for the I-th item: 0, 1 ...... N [I] parts.
Important: The DP [I] [J] indicates the maximum value of placing the first I items into a backpack with a capacity of J.
State transition equation: DP [I] [J] = max {DP [I-1] [V-K * V [I] + K * C [I] | 0 <= k <= N [i]}; (K indicates that K items are placed in the I-th item );
Core code:
For (I = 1; I <= N; I ++ ){
For (j = V; j> = 0; j --){
For (k = 1; k <= N [I]; k ++ ){
If (j> = K * V [I])
DP [I] [J] = max (DP [I-1] [V-K * V [I] + K * C [I])
}
}
8: (two-dimensional backpack problems)
The two-dimensional bag problem refers to: for each item, there are two different charges; the two costs must be paid at the same time when this item is selected; there is a maximum charge (Backpack capacity) for each price ). Ask how to select an item to maximize the value. Set these two costs to price 1 and price 2, respectively. The two costs required for item I are a [I] and B [I]. The maximum value (two types of backpack capacity) can be paid at two costs: V and U. The value of an item is W [I].
The fee is added to one dimension. You only need to add one dimension to the status. If f [I] [V] [u] is set, it indicates the maximum value that can be obtained when the cost of the first I item is V or U.
The state transition equation is:
F [I] [V] [u] = max {f [I-1] [V] [u], f [I-1] [V-A [I] [U-B [I] + W [I]}
9 (maximum sub-segments and problems (and differences between the maximum sub-sequences ))
Given a sequence of A1, A2, a3......;
Requirement: Find a sub-segment and the maximum value in the sequence.
DP [I] indicates that the end ends with the I element, and finds the maximum number of consecutive arrays ending with the I element and DP [I]
There are:
1 If DP [I-1]> 0, whatever the AI value, there is DP [I] = DP [I-1] + AI;
2 If DP [I-1] <= 0; discard, re-order DP [I] = AI; (because DP [I-1] is negative, No matter why the AI value is added, it will decrease)
State transition equation: DP [I] = DP [I-1] + AI (DP [I-1]> 0)
DP [I] = AI (DP [I-1] <= 0)
12 (maximum M sub-segments and)
When one dimension is added to the restriction condition, the State can be increased to one dimension to transfer the state.
End with DP [I] [J] and use J sub-segments to reach the maximum value (this one-dimensional State corresponds to a new restriction !) This makes it easy to write
State transition equation:
DP [I] [J] = max {DP [I-1] [J] + A [I], DP [I-K] [J-1] + A [I]} (J-1 <= k <n-M + J ).
1 DP [I-1] [J] + A [I] (include the I-th element in the last sub-segment)
2 DP [I-K] [J-1] + A [I], J-1 <= k <n-M + J (element I is a substring)
13 matrix concatenation
Problem description: Given a matrix of a sequence, it is required to find a matrix concatenation order to minimize the number of concatenation times.
Idea: Build a recursive expression and use the Dynamic Programming Method (M [I] [J] to represent the optimal solution between the I matrix and the J matrix, I * j * k multiplication is required for two matrices M (I, j) * s (j, k)
1 obviously, if I = J, M [I] [J] contains a matrix, and the number of times to be calculated is 0;
2 If I <J, then M [I] [J] = min {M [I] [k] + M [k + 1] [J] + P [I-1] * P [k] * P [J]}, K is wandering between I and j, So I <= k <J;
3. When calculating M [I] [J] To find M [I] [k] and M [k + 1] [J, M [I] [k] and M [k + 1] [J] have been calculated.
So there is a dynamic transfer equation:
M [I] [J] = {0, I = J };
M [I] [J] = {min {M [I] [k] + M [k + 1] [J] + P [I-1] * P [k] * P [J]}, i! = J };
M [1] [N] is the final solution.
The White Book wrote: there is no problem with the memory of China search, but if we want to write a recurrence, no matter how I or J increments or decreases, it is true. The correct method is to follow the ascending order of J-I, because the value of the long interval depends on the value of the short interval.
Template:
Int DP [maxn] [maxn]; // minimum storage count
Int s [maxn] [maxn]; // storage breakpoint, used on the output
Core code:
Int I, j, TMP;
For (L = 2; L <= N; l ++) {// The length of J-I. Since the length of 1 is the same matrix, 0 is not required.
For (I = 1; I <= N-L + 1; I ++) {// because J-I = L-1, the maximum value of J is n, therefore, the maximum I value is n-L + 1;
J = I + L-1; // because J-I = L-1, then J = L + I-1
DP [I] [J] = DP [I + 1] [J] + R [I] * C [I] * C [J]; // initialization, k = I;
S [I] [J] = I;
For (k = I + 1; k <j; k ++) {// cyclically enumerated k I <k <j
TMP = DP [I] [k] + dp [k + 1] [J] + R [I] * C [k] * C [J];
If (DP [I] [J]> TMP ){
DP [I] [J] = TMP; // update to the minimum value
S [I] [J] = K;
}
}
}
}
Output:
// Recursive call output
Void output (int I, Int J ){
If (I = J ){
Printf ("A % d", I); // when the two are equal, output A without Recursion
Return; // return the previous Layer
}
Else {
Printf ("(");
Output (I, S [I] [J]);
Printf ("X ");
Output (s [I] [J] + 1, J );
Printf (")");
}
}
14 intervals DP
The interval dynamic planning problem is generally considered. For each interval, their optimal values are obtained from the Optimal Values of several smaller intervals. It is an application of the divide and conquer idea, divide an interval problem into smaller intervals until the intervals composed of one element, enumerate their combinations, and obtain the optimal value after merging.
1 set f [I, j] (1 <= I <= j <= N) to represent the minimum cost of adding numbers in the interval [I, j]
2. minimum interval f [I, I] = 0 (a number cannot be merged, and the cost of merge is 0)
3 each time, K (I <= k <= J-1) is used to divide the interval into two sections: [I, K] and [k + 1, J ].
4 interval DP template, code
For (INT p = 1; P <= N; P ++) {// P is the length of the interval, as a stage
For (INT I = 1; I <= N-P; I ++) {// I is the starting point of the exhaustive range
Int J = I + P; // J indicates the end point of the interval.
For (int K = I + 1; k <j; k ++) // status transfer
DP [I] [J] = min {DP [I] [k] + dp [k + 1] [J] + W [I] [J]}; // This is a specific state transition equation.
Or DP [I] [J] = max {DP [I] [k] + dp [k + 1] [J] + W [I] [J]}; // calculate the maximum value
Or DP [I] [J] = min {DP [I] [k] + dp [k] [J] + W [I] [J]} // some must be K does not start with k + 1
}
}