Maximal continuous product substring
Title Description: Given a sequence of floating-point numbers, taking the value of the maximal product continuous substring, for example -2.5,4,0,3,0.5,8,-1, the maximum product substring fetched is 3,0.5,8. That is, in the above array, the product of the 3,0.5,8 3 numbers 3*0.5*8 = 12 is the largest, and is continuous.
Analysis:
Notice the difference between the substring and the word sequence.
SUBSTRING: is a continuous part of a string.
Subsequence: The sequence is not changed in the string, but the individual elements are removed to a new sequence.
1, the product of the continuous number, the simplest is the exhaustive method, is the interval [I...J] between the number of the product, and the maximum value of max, and finally output max. But the complexity of time is very large almost O (n^2).
2, in fact, this is somewhat similar to the maximum subarray and problem, the maximum subarray and when, when traversing, if sum <0,sum = Data[i].
However, there is a difference in the product, because at this moment the product is negative, in the next moment the product becomes positive, so the simple judgment of the method of <0 is not feasible, we can declare two variables, a product is the largest, a product is the smallest, both are dynamic changes, The maximum of the product may be multiplied by a number to become the smallest, and the product with the smallest possible becomes maximum.
3, you can use the method of dynamic planning, assuming that the array is A[],max represents the product value of the maximal contiguous substring ending with a, Min represents the product value of the smallest substring ending with a:
Max = max{a,max[i-1]*a,min[i-1]*a};
Min = min{a,max[i-1]*a,min[i-1]*a};
Initial state: max[1] = min[1] = a[0];
1, poor lifting method
#include <iostream>using namespace Std;DoubleMax_product (Double* Data,intN) {Double Max=0;intStart,end;intI,j;DoubleResult for(i =0;i< n;i++) {result = Data[i]; for(j = i+1; j<n;j++) {result*= data[j];if(Result >Max) {Max= result; start = i; End =j; } } }return Max;//Interval is [Start,end]}
Method Two:
DoubleMax_prod (Double*data,intN) {intIDoubleMaxcurrent =1;//Current maximum product DoubleMaxprod =1;Maximum product of//save DoubleMincurrent =1;DoubleMinprod =1; for(i =0;i< n;i++) {maxcurrent *= data[i]; Mincurrent *=data[i];if(Mincurrent > Maxprod) maxprod = mincurrent;if(Maxcurrent > Maxprod) maxprod = maxcurrent;if(Mincurrent < Minprod) Minprod = mincurrent;if(Maxcurrent < Minprod) Minprod = maxcurrent;if(Mincurrent > Maxcurrent) swap (mincurrent,maxcurrent);if(Maxcurrent <1)//Key, initialized to 1, if the maximum value is smaller than 1, reset to 1, a bit similar to the maximum subarray and. Maxcurrent =1; }returnMaxprod;}
Method Three: Dynamic programming
DoubleFuncDouble* Data,intN) {Double*maxa = newDouble[N];Double*mina = newDouble[N]; maxa[0] = mina[0] = data[0];DoubleMax = maxa[0];intI for(i =1;i< n;i++) {Maxa[i] =Max(data[i],maxa[i-1]*data[i],mina[i-1]*data[i]); Mina[i] =min(data[i],maxa[i-1]*data[i],mina[i-1]*data[i]); Max =Max(Max,maxa[i]); }returnMax;}
Extension: The maximum value of the product in the maximum n-1 number combination
Title Description:
Given an integer array of length N, only multiplication is allowed to calculate the maximum value of the product in the combination of any number (N-1).
such as: a[] = { -1,2,3,4}; The maximum product is 2*3*4 = 24.
Analysis:
is actually the combined product of the number of calculations (N-1), assuming that the first element is excluded from the product, the remaining elements are multiplied. We can find the combination of this N-1 number, calculate their product, and compare the size, a total of N (N-1) combination, the total time complexity is O (n^2), this time complexity is too high.
We can use the idea of space-time, taking element I as the dividing point, dividing the original array into two parts, constructing the array s[i] to represent the product of the first element of the array, T[i] representing the product of the array (n-i) after the element, t[i] = t[i+1]* A[i];
Set Array P[i] The product of the other N-1 elements, in addition to the first element,
P[i] = s[i-1]*t[i+1].
Need to scan the array two times from the end of the tail to get the array s[] and t[], and then linear time to get p[], and then traverse p[], to get the maximum value.
#include <iostream>using namespace STD;intMax_prod (intAintN) {intIintProd1, prod2; Prod1 = PROD2 =1;intMax =0;int*s =New int(N);int* t =New int(N);int*p =New int(N); for(i =0;i< n;i++)//Get s[i]{prod1 *= a[i]; S[i] = prod1; } for(i = n1; i>=0; i--) {prod2 *= a[i]; T[i] = prod2; } for(i=0;i< n;i++) {P[i] = s[i-1]*t[i+1];if(P[i]>max) Max = P[i]; }Delete[]s;//Free memory Delete[]t;Delete[]p;returnMax;}intMain () {intdata[5]= {1,2,-3,1,3};cout<<"Max is"<< Max_prod (data,5) << Endl;return 0;}
String Edit Distance
Title Description: Given a source string and a target string, the source string can be done as follows:
1, insert a character at the given position
1, replace any character
3, delete any character
Write a program that returns the minimum number of operands so that the source string is equal to the target string, and the length of the source string and target string is less than 2000.
Analysis:
That is, from the source string to the target string, do the least action, the operation consists of three kinds, not once can choose one.
S[0...I] indicates the source string
T[0...J] represents the target string.
F[I,J] represents the minimum editing distance from s[0...i] to T[0...J].
You can use the idea of dynamic planning: operation is three kinds, add, delete, replace
F[i,j] = min {f[i-1,j]+1, f[i,j-1]+1, f[i-1][j-1]+ (s[i]== t[i]?0:1)}
F[i-1,j]+1: Represents the deletion of a number, F[i-1,j] represents the minimum editing distance s[0...i-1] to T[0...J], this time the source string has been able to become the target string, so you can delete the redundant s[i].
F[i,j-1]+1: Represents the addition of a number, f[i,j-1] represents the minimum editing distance of s[0...i] to T[0...j-1, has not been converted to the target string, and one less t[i], to add operations.
f[i-1,j-1]+ (S[i]==t[i]? 0:1): Represents a substitution operation, f[i-1,j-1] represents the minimum editing distance of s[0...i-1] to t[0...i-1, which has not yet been converted to the target string, and the source string is left, the key point is s[i] and T[i] is equal, if equal, indicates that the conversion operation has been completed, [f[i-1,j-1] is to be evaluated, if not equal, the source string needs to be replaced once, need to f[i-1,j-1]+1 operations.
#include <iostream>#include <stdio.h>#include <string.h>using namespace STD;intMinintAintBintc) {if(A > B) {if(b > C)returnCElse returnb }Else{if(A > C)returnCElse returnA }}intMin_change (Char* SRC,intNChar* Dest,intm) {if(src = = NULL | | dest = NULL | | n<0|| m<0)return 0;Static intf[ -][ -];//Stack memory Limited, note add static intI,j; for(i =1;i< n;i++) {if(src[0] = = dest[0]) f[0][0] =0;Elsef[0][0] =1;if(src[i]== dest[0]) f[i][0] =i;Elsef[i][0] =i+1; } for(i =1;i< m;i++) {if(Dest[i] = = src[0]) f[0][i] =i;Elsef[0][i] =i+1; }intResult for(i =1; i<n;i++) { for(j=1; j<m;j++) {F[i][j] = min (f[i-1][j]+1, f[i][j-1]+1, f[i-1][j-1]+ (Src[i]==dest[j]?0:1)); result = F[i][j]; } }returnResult;}intMain () {Chara[Ten] ="BCEFG";Charb[Ten] ="ABEDG";cout<<"The cout is"<< Min_change (A,5B5) << Endl;return 0;}
Algorithm Learning (12) maximum continuous product substring, string edit distance