我得到一个错误:表达式必须在第4行上具有一个常量值(int cost [n] [n]),并在此基础上进一步出错,即“数组类型int [n] [n]是不可分配的” “。 我该如何解决?
int optimalSearchTree(int keys[], int freq[], int n)
{
/* Create an auxiliary 2D matrix to store results of subproblems */
int cost[n][n];
for (int i = 0; i < n; i++)
cost[i][i] = freq[i];
for (int L = 2; L <= n; L++)
{
// i is row number in cost[][]
for (int i = 0; i <= n - L + 1; i++)
{
// Get column number j from row number i and chain length L
int j = i + L - 1;
cost[i][j] = INT_MAX;
// Try making all keys in interval keys[i..j] as root
for (int r = i; r <= j; r++)
{
// c = cost when keys[r] becomes root of this subtree
int c = ((r > i) ? cost[i][r - 1] : 0) +
((r < j) ? cost[r + 1][j] : 0) +
sum(freq, i, j);
if (c < cost[i][j])
cost[i][j] = c;
}
}
}
return cost[0][n - 1];
}
答案 0 :(得分:-1)
在c ++中创建动态n * n大小的2d数组的标准方法是这样的
std::vector<std::vector<int> > cost(n,std::vector<int>(n));