Note: make sure that you also state and justify the running times of your algorithm.
In this problem, Professor Midas is not only given the location of each gas station along Interstate 80. He is also given the price per gallon of gas at each gas station. His new goal is to minimize the cost of gasoline for the trip rather than the number of stops.
* a b c a b b a b c b a c a c c
According to this table, a * b = b and b * a = c. Note that * is neither commutative nor associative.
Devise an algorithm which given a list of symbols x1, ..., xn such that each xi is in {a, b, c}, determines whether it is possible to parenthesize x1 * x2 * ... * xn in such a way that the resulting expression evaluates to a. If so, your algorithm should also produce such a parenthesization.
For example, for b * b * b * b * a, your algorithm should return "yes" because ((b * (b * b)) * (b * a)) = a. Note that the solution does not have to be unique. For example, (b * (b * (b * (b * a)))) is also equal to a.
Remember to argue that your algorithm is correct and to state and justify the running time of your algorithm. For full credit, your algorithm must be reasonably fast.