Maximum continuous product substring

Source: Internet
Author: User
1/* 2 A = ",-,-3", to obtain the maximum continuous product substring, there are three methods, Method 1: using dynamic programming method, it is the easiest to understand and implement. Method 2: Use the three ideas of dynamic planning, but there is no need to save two array spaces. Method 3: Use the Record maximum value, minimum value method 4 */5 6/* 7 dynamic planning method, two arrays, maximum and minimum, max [I] is the maximum continuous product of the sequence ending with I. 8 maxnum [I] = max {maxnum [I-1] * A [I], minnum [I-1] * A [I], a [I]}; 9 minnum [I] = min {maxnum [I-1] * A [I], minnum [I-1] * A [I], a [I]}; 10 maxnum [0] = minnum [0] = A [0]; 11 */12 # include <iostream> 13 using namespace STD; 14 # include <string> 15 # include <algorithm> 16 double maxmultiply (const double * STR, int N) 17 {18 int size = N; 19 if (size = 0) 20 return 0; 21 double * maxnum = new double [size]; 22 if (maxnum = NULL) 23 exit (1); 24 double * Minnum = new double [size]; 25 if (minnum = NULL) 26 exit (1); 27 double result = STR [0]; 28 maxnum [0] = minnum [0] = STR [0]; 29 for (INT I = 1; I <n; I ++) 30 {31 maxnum [I] = max (maxnum [I-1] * STR [I], minnum [I-1] * STR [I]), STR [I]); 32 minnum [I] = min (maxnum [I-1] * STR [I], minnum [I-1] * STR [I]), STR [I]); 33 If (maxnum [I]> result) 34 result = maxnum [I]; 35} 36 Delete [] maxnum; 37 Delete [] minnum; 38 return result; 39} 40 41/* 42 Dynamic Planning But two variables are used to save the max and Min sequences. 43 */44 double maxmultiply2 (const double * a, int N) 45 {46 if (a = NULL) 47 return 0; 48 double result = A [0]; 49 double maxnum = A [0], minnum = A [0]; 50 for (INT I = 1; I <n; I ++) 51 {52 double tmpmax = maxnum * A [I]; 53 double tmpmin = minnum * A [I]; 54 maxnum = max (tmpmax, tmpmin ), A [I]); 55 minnum = min (tmpmax, tmpmin), a [I]); 56 If (maxnum> result) 57 result = maxnum; 58} 59 return result; 60} 61 62 // method 3, using methods similar to observation and induction, maxcurrent, mincurrent, maxproduct, and minproduct. Note that if maxcurrent is less than 1, in fact, it is better to compare 63 // with yourself, but it is still the same, there is no need to write 64 double maxmultiply3 (const double * a, int N) 65 {66 if (a = NULL) 67 return 0; 68 double maxcurrent = 1; 69 double mincurrent = 1; 70 double maxproduct = 1; 71 double minproduct = 1; 72 for (INT I = 0; I <N; I ++) 73 {74 maxcurrent * = A [I]; 75 mincurrent * = A [I]; 76 if (maxcurrent> maxproduct) 77 maxproduct = maxcurrent; 78 If (mincurrent> maxproduct) 79 maxproduct = mincurrent; 80 If (maxcurrent <minproduct) 81 minproduct = maxcurrent; 82 If (mincurrent <minproduct) 83 minproduct = mincurrent; 84 If (mincurrent> maxcurrent) 85 swap (mincurrent, maxcurrent); 86 If (maxcurrent <1) 87 maxcurrent = 1; 88} 89 return maxproduct; 90} 91 92 int main () 93 {94 double A [] = {-2.5, 0.5, 3, 8,-1}; 95 int n = 7; 96 cout <maxmultiply3 (A, n) <Endl; 97 system ("pause"); 98}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.