Read the time to see a problem, think very interesting, I want to remember.
The problem is this, for a sequence a1,a2,... An, which makes Σak the largest value, wherein 1≤i≤k≤j≤n. For example, for sequences ( -2,11,-4,13,-5,-2), the answer is 20 (from A2 to A4).
This is a very simple question, and one of the most intuitive ideas is to iterate through the initial and end points of all possible subsequence sequences, where I is the initial point of the Subsequence and J is the end point of the subsequence.
int max_sum = 0 ; for (int i =0 ; i<seq.size (); I++ int this_sum = 0 ; for (int J =i;j<seq.size (); j+ + += Seq[j]; if (Max_sum<this_sum) {max_sum = This_sum; } }}
In the book part, the discussion of this question is not how to achieve, but through it tells us the complexity of the different algorithms. The complexity of the above method is O (N2) is mainly because of the two for loop, then whether there is a smaller complexity of the method? The book tells us two smaller complexity methods (Method 1 is more classic but more complex, lazy classmates can directly see Method 2).
1. Using the idea of dichotomy, the original problem is decomposed to consider the left half-sequence and the largest, right-half-sequence and maximum, as well as the sequence and the largest problem containing the midpoint value of the sequence. This translates the subsequence and problem of length n into two sub-sequences and problems with a length of N/2, and a question of the sequence and maximum values that contain the midpoint values.
such as a subsequence ( -2,11,-4,13,-5,-2), translates to ( -2,11,-4), (13,-5,-2), and a sequence containing ( -4,13) points. In this example, the left half of the sequence has a maximum value of 11 and the right half is 13. For sequences containing ( -4,13), we consider the sequence and maximum values of 4 as the end point and 13 for the start point, respectively, 7 (11,-4), 13, and then compare 7,13,7+13 to know that the sequence and maximum value containing ( -4,13) is 7+13=20 (11,-4,13 )。 Finally, we compare the sequence values of the three parts 11,13,20 that the partial value of the containing ( -4,13) is 20.
The algorithm can reduce the complexity of the algorithm to O (Nlog2 (N)), where the n/2 of the three parts of the transformation of the sub-sequence and the problem using a recursive method to invoke the original function, while the inclusion of the midpoint value of the fixed initial and end points are calculated by the maximum value after the comparison can be obtained. This method realizes this function by means of two-point and recursive method, and reduces the complexity of the algorithm, if there is no subsequent method 2, this will be a good solution to the problem. Because the algorithm writes out the code is longer, and I am lazy, and we have a simpler and more effective method, it is not here to give the code of the algorithm, interested students can go to the book to see the corresponding code.
2. finally introduce the algorithm to solve this problem, the algorithm complexity is only O (N), only need to traverse a sequence, it can be achieved, and the idea is very simple!!
We mainly consider the starting and ending points of sub-sequences for subsequence problems. For the consideration of sub-sequences and the biggest problem, if the subsequence and less than 0, then the subsequence is not likely to be the beginning of the optimal subsequence, because the total can be improved with subsequent values. Therefore, if the subsequence and less than 0, the starting point should be set to the next. If the subsequence and greater than 0, then the subsequence should not be altered and, because the subsequence is associated with the new value added and should be greater than the new value added only. This extension comparison adds the front and rear maximum values and selects the larger as the end point. For example, for example ( -2,11,-4,13,-5,-2) analysis, then (start point, end Point, subsequence and, maximum) with the new value is added in order (0,0,0,0), 2 (0,0,-2,0), 11 (2,2,11,11), 4 (2,2,7,11), 13 ( 2,4,20,20),-5 (2,4,15,20),-2 (2,4,13,20)
I made a slight change to the code in the book (mainly by adding a few lines of code to find the starting and ending points) and put it below. You can try to verify that the conclusion is correct.
#include <iostream>#include<vector>using namespacestd;voidFind_max_subseq (Constvector<int> &seq) { intMax_value=0, subseq_value=0, start_pos=0, end_pos=0; for(intI=0; I<seq.size (); + +i) {Subseq_value+=Seq[i]; if(subseq_value>max_value) {Max_value=Subseq_value;
Start_pos = Tmp_pos; End_pos= i+1; } Else { if(subseq_value<0) {Subseq_value=0; if(I!= (Seq.size ()-1) ) {Tmp_pos=i+2; } }}} cout<<"Max_value:"<<max_value<<Endl; cout<<"Start_pos:"<<start_pos<<Endl; cout<<"End_pos:"<<End_pos;}intmain () {vector<int>seq; intLength_seq; cout<<"Input Sequence Length"<<Endl; CIN>>Length_seq; cout<<the input sequence value"<<Endl; intnum; for(intI=0; i<length_seq;++i) {cin>>num; Seq.push_back (num); } find_max_subseq (seq); return 0;}
In this way we reduce the algorithm complexity from O (N2) to O (n), for a large N value, the effect will be significantly improved. For us, more is to learn a train of thought, encounter problems can think more, although the first method is easy to think of, but if you think more, can find a third method will be more happy.
#读书笔记--Data structure and algorithm analysis (c + +) maximum subsequence and problem.
Maximum sub-sequences and problems