algorithm - Minimize the sequence by putting appropriate operations ' DP' -


Given a sequence, say, 222, we put a '+' or '*' between each adjacent pair is. The higher priority is '+' on the '+'

The O / P string should be semantically smaller than the O / P string whose evaluation leads to the lowest value. Explanation:

2 + 2 + 2 = 6

Inp: 222

o / p: 2 * 2 + 2

/ P>

2 + 2 * 2 = 6

2 * 2 + 2 = 6

The third of its dictionary is the smallest.

I was wondering how to prepare a DP solution for this.

let DP [n] firstly we first n by using the elements. I will do a recursive implementation with the pseudocode (using the memoisation):

  int plot {if (index == N) 0; If (DP [index] has already been calculated) Return DP [Index]; Int results = incredibly large numbers; // one + sign result = min (result, input [index] + solution (index + 1)); // For continuous * icon int = input [index]; For (int i = index + 1; i & lt; n; i ++) {esr = input [i]; Result = minutes (result, Q + Hull (i + 1)); } Return DP [index] = results; }  

call it with solution (0);

You can easily reconstruct the subsequent solutions. I have not tested it, and I've missed the case of an edge in pseudocode but it should give you the right track.

  string reconfiguration (int index) {if (index == N) return ""; String result = ""; // For continuous * icon int = input [index]; String temp = tostrings (input [index]); For (int i = index + 1; i & lt; n; i ++) {esr = input [i]; Floating + = "*"; If (DP [index] == Q + DP [I + 1]) result = temporary + reconstruction (I + 1); } // put a + sign (if result == "") result = ToString (input [index]) + "+" + reconstruction (index + 1); Return result; } String result = Reconstruction (0);  

P.S. Pardon.


Comments