The method of using C language to find the product of maximal continuous sub-series _c language

Source: Internet
Author: User

title : Give a floating-point number sequence, take the maximum product continuous substring value, such as -2.5,4,0,3,0.5,8,-1, then remove the maximum product continuous substring is 3,0.5,8. That is, the product 3*0.5*8=12 of the 3 numbers in the above array, 3 0.5 8, is the largest and contiguous.

Reminder : This maximum product serial substring is different from the maximum product subsequence, do not confuse, the former substring requires continuous, the latter subsequence does not require continuous. In other words: the difference between the longest common substring (longest commonsubstring) and the longest common subsequence (Longestcommon Subsequence,lcs):


A substring (Substring) is a contiguous part of a string, and a subsequence (subsequence) is a new sequence that is derived from the sequence of sequences and removes any elements from the sequences.
In a nutshell, the characters of the former (substring) must be positioned continuously, while the latter (the subsequence LCS) is not necessary. For example, the longest common substring of the string "ACDFG" and "AKDFC" is "DF", while their longest common subsequence LCS is "ADF", which can be resolved by using dynamic programming.

Answer:

Solution One , exhaustive, all the calculation combination:
Perhaps, the reader will naturally think of the maximum product subsequence problem similar to the maximum number of sub arrays and problems: http://blog.csdn.net/v_JULY_v/article/details/6444021, you may immediately think of the simplest and most brutal way: Two for Loop direct polling.

 &NBSP   Solution Two , although similar to the maximum number of child arrays and problems, is actually handled in many different ways. Why, because there are positive and negative in the product subsequence and there may also be 0. We can simplify the problem by finding a subsequence in the array, making it the product of the largest, and finding a subsequence that makes the product the smallest (negative). Because although we only have a maximum product, but because of the existence of negative numbers, we also find these two products to do is convenient. That is, not only the maximum product is recorded, but also the minimum product is recorded. So, we let maxcurrent represent the candidate,mincurrent of the current maximum product, which represents the candidate of the current minimum product, while Maxproduct records the maximum value of all the maximum product candidates so far. (the word candidate is because it is only possible to become the maximum/minimum product of a new round) because the product of the empty set is defined as 1, and the maxcurrent,mincurrent,maxproduct is assigned 1 before the array is searched.
Suppose that at any moment you have the candidates of the two Max/min products of maxcurrent and Mincurrent, after the new read into the array element x (i), The new maximum product candidate may only be the larger of the product of Maxcurrent or mincurrent and x (i) if x (i) <0 causes maxcurrent<mincurrent, You need to exchange the values of these two candidates. When maxcurrent<1 at any time, because 1 (empty set) is better than maxcurrent candidate, so update maxcurrent is 1, similar can update mincurrent. Any time maxcurrent if the maxproduct is bigger than the best, update maxproduct.

solution three , in addition to the above similar maximum sub arrays and the solution, can also be directly solved by dynamic programming (in fact, the solution is a dynamic programming in nature, but the specific form of solving problems shown in the next two different than the solution.) The difference lies in the following solution two will write the dynamic programming problem in the classical common DP equation, and the solution is directly solved. The specific solution is as follows:
Assuming that the array is a[], the direct use of the dynamic return solution, taking into account the possibility of negative numbers, we use Max to represent the end of a maximum continuous substring of the product value, with Min to the end of a the smallest substring of the product value, then the state transition equation is:
Max=max{a, Max[i-1]*a, min[i-1]*a};
Min=min{a, Max[i-1]*a, min[i-1]*a};
Initial state is max[1]=min[1]=a[1].


Let's look at a specific ACM topic
Topic

Topic Description:
Given a floating-point sequence (which may have positive, 0, and negative numbers), the product of one of the largest sequential subsequence sequences is obtained.
Input:
The input may contain more than one test sample.
The first row of each test sample contains only positive integer n (n<=100000), representing the number of floating-point numbers.
The second line enters n floating-point numbers separated by a space.
The input data guarantees that all the numbers multiplied within the range of the double-precision floating-point number.
Output:
For each test case, the product of the largest sequential subsequence in the output sequence, if the product is a floating-point number, keep 2 decimal digits, if the maximum product is negative, output-1.
Sample input:

  7 
  -2.5 4 0 3 0.5 8-1 
  5 
  -3.2 5-1.6 1 2.5 
  5 
  -1.1 2.2-1.1 3.3-1.1 

Sample output:

 
  8.78 


Ideas
the maximal continuous subsequence product and the maximal continuous subsequence and the difference, here we first recall the optimal solution structure of the maximal continuous subsequence and the following sequence:

Maximum contiguous subsequence and
We use Sum[i] to represent the maximum contiguous sequence and the end of a arr[i, then the state transfer equation is:

Product of the maximal continuous sub series
Consider the existence of a negative number (PS: Negative will be positive), so we use two auxiliary arrays, Max[i] and min[i],max[i] to represent the arr[i] end of the maximum sequence of the product, Min[i] to represent the end of the arr[i of the minimum sequence of the product, The state transition equation is therefore:

and

With the state transition equation, DP code is very easy to achieve, see here still do not understand the classmate, I suggest you spend a little more time on the algorithm to learn it!

AC Code

 #include <stdio.h> #include <stdlib.h> double maxnuminthree (double A, double b, double c) { 
    Double Max; Max = (A > B)? 
    A:B; max = (max > C)? 
    
    MAX:C; 
  return Max; 
    Double Minnuminthree (double A, double b, double c) {double min; Min = (A < b)? 
    A:B; Min = (min < c)? 
    
    MIN:C; 
  return min; 
    int main (void) {int i, n; 
    
    Double *arr, *max, *min, res; 
      while (scanf ("%d", &n)!= EOF) {arr = (double *) malloc (sizeof (double) * n); 
      Max = (double *) malloc (sizeof (double) * n); 
      Min = (double *) malloc (sizeof (double) * n); 
    
      for (i = 0; i < n; i + +) scanf ("%lf", arr + i); 
      Dynamic programming for maximal continuous subsequence product max[0] = min[0] = res = arr[0]; 
        for (i = 1; i < n; i + +) {Max[i] = Maxnuminthree (Arr[i], max[i-1] * arr[i], min[i-1] * arr[i)); Min[i] = Minnuminthree (Arr[i], max[i-1] * arr[I], min[i-1] * arr[i]); 
      if (Max[i] > Res) res = max[i]; } if (res >= 0) {//To determine if floating-point number if (RES-(int) res = = 0) printf ("%d\n", (int) r 
        ES); 
      else printf ("%.2lf\n", res); 
      else {printf (" -1\n"); 
    Free (arr); 
  return 0; 
 }

       
   /******************************************
        problem:1501
         User:wangzhengyi
        language:c
         result:accepted
        time:110 Ms
        memory:4964 KB
    ********************************* / 

Related Article

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.