TODO: What exactly is a dynamic plan?
Ref:http://www.cnblogs.com/waytofall/archive/2012/04/10/2439820.html
I only consider how to produce larger subgroups and:
Assuming that the first node is processed:
1. Consider whether the I node can make subgroups and grow larger
A) If the I node is greater than 0, then the maximum subgroup and is the array that was previously obtained and the I-node value
b) If I node is less than 0 (equal to 0, the value can take I, also can not take I, it does not matter), when the sum of the largest sub-array and is the front of the child Group and
2. Then consider if the maximum value should be updated
A) If the current subgroup and is greater than the maximum and, then the update
int//Dynamic programming method
Get_max_sub (int a[], int len)
{
int max_sum, cur_sum;
int i;
Max_sum = Cur_sum = A[0];
i = 0;
while (++i < Len) {
if (Cur_sum > 0) {
Cur_sum + = A[i];
} else {
Cur_sum = A[i];
}
if (Cur_sum > Max_sum) {
Max_sum = Cur_sum;
}
}
return max_sum;
}
II refinement consider the front and back boundaries of the Subarray
1. Because the sub-array and is negative, the maximum value is the I node, then the left edge is moved to the I node.
2. When I node is negative, (thinking mined area!!) when you move to the right? Of course, the sub-array and the larger, then moved (the child and the larger, the sub-array expanded)
int//Dynamic programming method
Get_max_sub (int a[], int len, int <, int &rt)
{
int max_sum, cur_sum;
int i;
Max_sum = Cur_sum = A[0];
lt = rt = 0;
i = 0;
while (++i < Len) {
if (Cur_sum > 0) {
Cur_sum + = A[i];
} else {
Cur_sum = A[i];
lt = i;
}
if (Cur_sum > Max_sum) {
Max_sum = Cur_sum;
RT = i;
}
}
return max_sum;
}
Summarize:
1. The depth of thinking is a little deeper, from less and more
2. Get rid of the thinking inertia and avoid the minefield.
When considering the maximum Subarray, consider whether the center is a historical sub-array and whether it is a positive or negative (that is, the sub-array and is the current first I node or the first node plus the previous and then), rather than just consider adding the I-node idea array is larger or smaller (intuitive/inertial thinking into the mined area)
Dynamic planning solves the problem of "maximum sub-array"