Dynamic Programming for inserting R multiplication numbers

Source: Internet
Author: User

Insert 4 multiplication numbers using the exhaustive method. Visit: http://www.vcworld.net/bbs/viewthread.php? Tid = 8580 & extra =

I. Question proposal

Insert the r multiplication number (1 <= r <n <16) In a number string consisting of n numbers and divide it into R + 1 integers, find a method to insert a multiplication number to maximize the product of the R + 1 integer.

For example, for a given number of 847313926 strings, how do I insert r = five multiplication numbers to maximize the product?

 

Ii. Dynamic Planning and Design

Generally, it is not suitable to insert R multiplication numbers using exhaustive examples. Note that inserting R multiplication numbers is a multi-stage decision-making problem. It is appropriate to use dynamic planning to solve the problem.

1. Establish a recursive relationship

If f (I, k) is set, it indicates the maximum value of the product of K multiplication numbers inserted in the first I bits, a (I, j) indicates the J-I + 1 (I <= J) integer from number I to number J.

To look for Recursive Relations, let's look at an instance first: for a given 847313926, how to insert r = five multiplication numbers to maximize the product? Our goal is to obtain the optimal value F (9, 5 ).

① If four multiplication numbers have been inserted into the first eight digits, the maximum product is F (8, 4) * 6;

② If four multiplication numbers have been inserted into the first seven digits, the maximum product is F () * 26;

③ If four multiplication numbers have been inserted into the first six digits, the maximum product is F (6, 4) * 926;

④ If four multiplication numbers have been inserted into the first five digits, the maximum product is F (5, 4) * 3926;

The maximum value is F (9, 5 ).

To calculate F (8, 4 ):

① If three multiplication numbers are inserted into the first seven digits, the maximum product is F () * 2;

② If three multiplication numbers have been inserted into the first six digits, the maximum product is F (6, 3) * 92;

③ If three multiplication numbers are inserted into the first five digits, the maximum product is F (5, 3) * 392;

④ If three multiplication numbers are inserted into the first four digits, the maximum product is F (1392;

The maximum value of the above four values is F (8, 4 ).

Generally, in order to obtain f (I, k) and evaluate the first I digit of a numeric string, set the first J (k <= j <I) in the number has been inserted on the basis of the K-1 multiplication number, after the J number Insert the T multiplication number, obviously at this time the maximum product is F (J, k-1) * A (J + 1, I ). So we can get the recursive relationship:

F (I, K) = max (f (J, k-1) * a (J + 1, I) (k <= j <I)

When the first J numbers are not inserted with the multiplication number, the value is obviously an integer composed of the first J numbers. Therefore, the boundary value is:

F (J, 0) = A (1, J) (1 <= j <= I)

In the program design, the array a is omitted and replaced by the variable D.

2. Recursive calculation of the optimal value

For (D = 0, j = 1; j <= N; j ++) {<br/> d = D * 10 + B [J-1]; // assign each digit of the input numeric string to array B <br/> F [J] [0] = D; // calculate the initial value f [J] [0] <br/>}</P> <p> for (k = 1; k <= r; k ++) <br/> {<br/> for (I = k + 1; I <= N; I ++) <br/> for (j = K; j <I; j ++) {<br/> for (D = 0, u = J + 1; U <= I; U ++) <br/> d = D * 10 + B [U-1]; // calculate d as a (J + 1, I) <br/> If (F [I] [k] <F [J] [k-1] * D) // calculate f [I] [k] By recursion <br/> F [I] [k] = f [J] [k-1] * D; <br/>}< br/>}

 

3. Construct the optimal solution

In order to print the product of the inserted multiplication number, set the T (K) and C (I, K) Arrays for the location of the annotation, where C (I, K) is the position of the K th multiplication Number of the corresponding F (I, K), and T (k) indicates the position of the K th multiplication number "*", for example, T (2) = 3, indicating that the first "*" number is behind the first number.

When F (I, K) = f (J, k-1) * D is assigned to an array element, C (I, K) = J is assigned to f (I, k) The position of the k-th multiplication number is J. Based on the result that the nth multiplication position T (r) = C (n, R) = J of F (n, R) is obtained) (1 <= k <= R-1) can be applied to the next type of backward production:

T (K) = C (t (k + 1), K)

Based on the value of the T array, you can print the multiplication formula of the inserted multiplication number obtained by the surface area in the form of characters.

 

3. Program Implementation

 // Insert R multiplication numbers into a number to maximize the product <br/> // use dynamic programming to solve the problem <br/> # include <stdio. h> <br/> # include <string. h> </P> <p> int main () <br/> {<br/> char numstr [16]; <br/> int I, J, K, len, U, R, B [16], t [16], C [16] [16]; <br/> double F [17] [17], D; </P> <p> printf ("enter an integer:"); <br/> scanf ("% s", numstr ); <br/> printf ("Enter the number of inserted multiplication Numbers R:"); <br/> scanf ("% d", & R ); <br/> Len = strlen (numstr); <br/> If (LEN <= r) {<br/> printf ("the input integer is not enough or R is too large! /N "); <br/> return 0; <br/>}</P> <p> printf (" insert % d multiplication numbers in integer % S, maximum Product:/N ", numstr, R); <br/> for (D = 0, j = 0; j <= len-1; j ++) <br/> B [J] = numstr [J]-'0'; <br/> for (D = 0, j = 1; j <= Len; j ++) {<br/> d = D * 10 + B [J-1]; // convert a character in array B to a value <br/> F [J] [0] = D; // F [J] [0] assigns an initial value <br/>}</P> <p> for (k = 1; k <= r; k ++) <br/> for (I = k + 1; I <= Len; I ++) <br/> for (j = K; j <I; j ++) {<br/> for (D = 0, u = J + 1; U <= I; U ++) <br/> d = D * 10 + B [U-1]; <br/> If (F [I] [k] <F [J] [k-1] * D) {<br/> F [I] [k] = f [J] [k-1] * D; <br/> C [I] [k] = J; <br/>}</P> <p> T [R] = C [Len] [r]; <br/> for (k = r-1; k> = 1; k --) <br/> T [k] = C [T [k + 1] [k]; // reverse route the K * number. <br/> T [0] = 0; t [R + 1] = Len; <br/> for (k = 1; k <= R + 1; k ++) {<br/> for (u = T [k-1] + 1; U <= T [k]; U ++) <br/> putchar (numstr [U-1]); // output the optimal solution <br/> If (k <R + 1) <br/> putchar ('*'); <br/>}< br/> printf ("= %. 0f/N ", F [Len] [r]); // output the optimal value </P> <p> return 0; <br/>}

Selected from interesting C Program highlights

 

 

// Insert R multiplication numbers into a number to maximize the product <br/> // use dynamic programming to solve the problem <br/> # include <stdio. h> <br/> # include <string. h> </P> <p> const int maxn = 41; <br/> int N [maxn], Len; </P> <p> // NT... NZ integer synthesis <br/> long NN (int t, int Z) <br/>{< br/> int I; <br/> long a = N [T]; </P> <p> for (I = t + 1; I <= z; I ++) <br/> A = A * 10 + N [I]; </P> <p> return; <br/>}</P> <p> int main (void) <br/>{< br/> int I, N, C, K, T; <br/> long num, F [maxn] [maxn]; <br/> char s [maxn]; </P> <p> scanf ("% d", & N, & C); <br/> scanf ("% s", S ); <br/> Len = strlen (s); <br/> for (I = 0; I <Len; I ++) <br/> N [I] = s [I]-'0'; <br/> K = 0; <br/> F [0] [0] = N [0]; <br/> for (I = 1; I <Len; I ++) <br/> F [I] [0] = f [I-1] [0] * 10 + N [I]; <br/> K = 1; <br/> for (k = 1; k <= C; k ++) {<br/> for (I = K; I <Len; I ++) {<br/> long a =-1; <br/> for (t = k-1; t <I; t ++) {<br/> num = nn (t + 1, I); <br/> long B = f [T] [k-1] * num; <br/> if (a <B) <br/> A = B; <br/>}< br/> F [I] [k] =; <br/>}< br/> printf ("% d/N", F [Len-1] [c]); </P> <p> return 0; <br/>}

 

From ACM/ICPC Program Design Analysis

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.