Question

3. [25 points] Heres another example of using dynamic programming over intervals (like we saw with the RNA Secondary Structure problem) Youre given an arithmetic expression consisting of positive integers separated by +s and s but without parentheses, and the goal is to determine the maximum possible value the expression could have by inserting parentheses in some way. For example, with 9-1+9-1 the max value is (9-1)(9-1) 16 (by the way, ((9-1)+9)-1 is an alternative parenthesization that achieves this value), whereas for 1-9+ 1-9 the max value is 1-((9 + 1)-9) = 0. For 1+8-3+9-5+6+7-2-4the max value is 1- ((((8-(3+9))-5)-(6+7))-(2+4))- 29 The input will be represented as two arguments: the list of numbers and the list of operations; e.g., 1+8-3+9-5+6+7-2-1 s represented as a= [1, 8, 3, 9, 5, 6, 7, 2, 4] and b-[, -, +, -, +, +, -, -]. For each interval-say the numbers at indices i through j in a you should have two subproblems, one for that subexpressions ible value, and one for its minimum possible value. Solve the subproblems in

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <cassert>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
#include <cmath>
using std::vector;
using std::string;
using std::max;
using std::min;
long long eval(long long a, long long b, char op) {
if (op == '*') {
return a * b;
} else if (op == '+') {
return a + b;
} else if (op == '-') {
return a - b;
} else {
assert(0);
}
}
long long get_maximum_value(const string &exp) {
int length = exp.size();
int numOfnum = (length + 1) / 2;
long long minArray[numOfnum][numOfnum];
long long maxArray[numOfnum][numOfnum];
memset(minArray,0,sizeof(minArray)); // initialize to 0
memset(maxArray,0,sizeof(maxArray));
for (int i = 0; i < numOfnum; i++)
{
//The values on the main diagonal is just the number themselves
minArray[i][i] = std::stoll(exp.substr(2*i,1));
maxArray[i][i] = std::stoll(exp.substr(2*i,1));
}
for (int s = 0; s < numOfnum - 1; s++)
{
for (int i = 0; i < numOfnum - s - 1; i++)
{
int j = i + s + 1;
long long minVal = LLONG_MAX;
long long maxVal = LLONG_MIN;
// find the minimum and maximum values for the expression
// between the ith number and jth number
for (int k = i; k < j; k++ )
{
long long a = eval(minArray[i][k],minArray[k + 1][j],exp[2 * k + 1]);
long long b = eval(minArray[i][k],maxArray[k + 1][j],exp[2 * k + 1]);
long long c = eval(maxArray[i][k],minArray[k + 1][j],exp[2 * k + 1]);
long long d = eval(maxArray[i][k],maxArray[k + 1][j],exp[2 * k + 1]);
minVal = min(minVal,min(a,min(b,min(c,d))));
maxVal = max(maxVal,max(a,max(b,max(c,d))));
}
minArray[i][j] = minVal;
maxArray[i][j] = maxVal;
}
}
return maxArray[0][numOfnum - 1];
}
int main() {
string s;
std::cin >> s;
std::cout << get_maximum_value(s) << '\n';

}

#include <iostream>
Add a comment
Know the answer?
Add Answer to:
3. [25 points] Here's another example of using dynamic programming over intervals (like we saw with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Overview The purpose of this assignment is to practice functional programming in the Racket Programming Language...

    Overview The purpose of this assignment is to practice functional programming in the Racket Programming Language and to also reinforce the notion of the list as a universal data structure, by implementing various operations on binary search trees. Specification A binary search tree is a binary tree which satisfies the following invariant property: for any node X, every node in X's left subtree has a value smaller than that of X, and every node in X's right subtree has a...

  • Write down how the expression (A-B) V - (CA D)A (E-F) of wffs (an example is...

    Write down how the expression (A-B) V - (CA D)A (E-F) of wffs (an example is given below): Example: The expression (A A-B) V (CD) 1. A is a wff (propositional variable) 2. B is a wff (propositional variable 3. -B is a wff (obtained by applying to 2) 4. AA-B is a wff (obtained by applying A to 1 and 3) 5. (AA-B) is a wff (obtained by applying parentheses to 4) 6. C is a wff (propositional variable)...

  • use the example to answer 8,9,10&11 Here's an example: Balance the following redox reaction, which occurs...

    use the example to answer 8,9,10&11 Here's an example: Balance the following redox reaction, which occurs in acidic solution: Fe (aq)+ MnO4'(aq) -Fe (aq) + Mn (aq) Solution: +2 +7 +3 Step 1) +2 Fe2 (aq)+ MnOa (aq) Fe(aq) + Mn2 (aq) Fe (aq) MnO4 (aq) Mn2 (aq) 1 Fe on each side; 1 Mn on each side; no adjustment necessary Fe2 (aq) Fe 3'(aq) + e 5 e + MnO4(aq) Fe (aq) Fe (aq) + e (2+ on each...

  • Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.r...

    Please write a JAVA program according to following requirement. Thanks Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.random () generates a random integer greater than or equal to 0 and less than 1. The expression Math. random) 6generates random numbers between 0 and 5, simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair; that is, whether each possible...

  • 3.5 max number We will pass in a list of numbers your job is to find...

    3.5 max number We will pass in a list of numbers your job is to find the largest number in that list and outputted it’s index , not the actual value # Get our numbers from the command line import sys 4 numbers sys.argv[1].split',') Collagse) Challenges 5 numbers int(i) for i in numbers] 3. 5. Max number # Your code goes here We will pass in a list of numbers. Your job is to find the largest number in that...

  • Use log, 3 * 0.583, log, 5 0.821, and log, 7. 1.064 to approximate the value...

    Use log, 3 * 0.583, log, 5 0.821, and log, 7. 1.064 to approximate the value of the given logarithm to 3 decimal places. Assume thath > 0 and b + 1. log) 21 5. Write the logarithmic expression as a single logarithm with coefficient 1, and simplify as much possible. Assume that all variable expressions represent positive real numbers. log, (v) + log2 (x2 - 9) – log2 ( +3) - Write the logarithmic expression as a single logarithm...

  • 3. (8 points) Using the implementation of binary search tree operations we discussed in class, draw...

    3. (8 points) Using the implementation of binary search tree operations we discussed in class, draw the trees that result from the following operations: (a) Inserting 142, 400, 205, 127, 100, 320, 160, 141, and 110 into an initially-empty tree (in that order). (b) Deleting 142 from the tree you drew for part (a). 4. (8 points) Draw the unique binary tree that has a preorder traversal of 4, 1, 6, 3, 7, 5, 9, 2, 8 and an inorder...

  • For this assignment we are going to create the beginnings of a simple Grade Book. We...

    For this assignment we are going to create the beginnings of a simple Grade Book. We will revisit this assignment again once we learn some new concepts. For the purposes of this grade book you should first provide a menu with the following options: 1. Add a new course 2. Add a new student 3. Add a student to a course 4. Add grades for a student in a course 5. Print a list of all grades for a student...

  • PYTHON REQUIRED Bonus (4 points): Why was 6 afraid of 7? Because 7 89! Actually, this...

    PYTHON REQUIRED Bonus (4 points): Why was 6 afraid of 7? Because 7 89! Actually, this is mathematically impossible. "Cannibal numbers" as they're called can only eat numbers that are smaller than them, or the same size. After eating another number, the number that gets eaten disappears, and the number that ate them increases by 1 For example, given the following set of numbers, let's pretend that 9 is the cannibal. 127, 9,3, 8, 11] 9 could start by eating...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT