算法的括号前pression,以发挥其最大的价值括号、算法、价值、最大

2023-09-11 04:41:33 作者:爱笑男孩

我发现这个在查找动态规划问题。 您将得到如下形式的unparanthesized EX pression V0 O0 V1 O1 .... VN-1

I found this while looking up problems on dynamic programming. You are given an unparanthesized expression of the form V0 O0 V1 O1 .... Vn-1

我们必须把括号内最大化的整个前pression的价值的地方。

We have to put brackets at places which maximizes the value of the entire expression.

五世的是操作数和O是经营者。 在问题运营商的第一个版本,可*和+和操作数​​是积极的。 第二个版本的问题是完全通用的。

V's are the operands and O's are the operators. In the first version of problem operators can be * and + and operands are positive. Second version of problem is completely general.

对于第一个版本,我想出了DP的解决方案。

For the first version i came up with DP solution .

解决方案 - A [] =操作数数组 B〔] - 运算符阵 T(A [I,J]) - 的前pression最大值  T(A [0,N-1])= MAX在所有I {(T(A [0,1])爱T(A [1 + 1,N-1]))}

Solution - A[] = operands array B[] - operators array T(A[i,j]) - max value of expression T(A[0,n-1]) = max over all i {(T(A[0,i]) Oi T(A[i+1,n-1]))}

边界的情况下是微不足道的(当i = j)条。 我需要用下面的帮助 - 分析该算法的运行时间。如果存在一个更好的。

The boundary cases are trivial (when i = j). I need help with the following - Analyze the running time of this algorithm. And if there exists a better one.

推荐答案

这是比较容易分析计算 A [I,J] 从短距离的元素更远的距离。算法,看起来像:

It is easier to analyze calculation of A[i,j] elements from shorter ranges to longer ranges. Algorithm for that looks like:

# Initialization of single values
for i in 0, ..., n-1:
  A[i,i] = V[i]

# Iterate through number of operation
for d in 1, ..., n-1:
  # Range start
  for i in 0, ..., n-1-d:
    j = i + d
    A[i,j] = max( A[i,x] O_x A[x+1,j] for x in i, ..., j-1)

print 'Result', A[0,n-1]

由于 A [] 可以用一定的时间访问(阵列)比算法实现的是为O(n ^ 3)

Since A[] can be implemented with constant time access (array) than algorithm is O(n^3).