英语题翻译10

Matrix Chain Multiplication | JXNUOJ

Matrix multiplication problem is a typical example of dynamical programming.
Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of elementary multiplications needed strongly depends on the evaluation order you choose.
For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.
There are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).
The first one takes 15000 elementary multiplications, but the second one only 3500.
Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy.

Input consists of two parts: a list of matrices and a list of expressions.
The first line of the input file contains one integer n (1 <= n <= 26), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix.
The second part of the input file strictly adheres to the following syntax (given in EBNF):
SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

For each expression found in the second part of the input file, print one line containing the word "error" if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses.

翻译:

矩阵乘法问题是动态规划的一个典型例子。假设你需要计算一个像A*B*C*D*E的表达式,其中A,B,C,D和E都是矩阵。由于矩阵乘法是关联的,因此执行乘法的顺序是任意的。然而,需要的基本乘法的数量很大部分取决于你选择的计算顺序。例如,让A成为一个50*10的矩阵,B成为一个10*20的矩阵,并且C成为一个20*5的矩阵。有两种不同的策略计算出A*B*C,也就是 (A*B)*C和 A*(B*C)。第一种需要15000个基本乘法,而第二个只需要3500个。你的工作是写一个程序确定给定的计算策略需要的基本乘法数。

输入由两部分组成:矩阵列表和表达式列表。输入文件的第一行包含一个整数n(1 <= n <= 26),代表第一部分矩阵的数量,接下来的n行每一行包含一个大写字母,指定矩阵的名称,还有两个整数,指定矩阵的行数与列数。输入文件的第二部分严格遵守以下语法(在 EBNF 中给出):SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

对于输入文件第二部分中找到的每个表达式,如果表达式的计算由于不匹配的矩阵而导致错误,则打印包含单词“error”的一行,否则打印一行其中包含以括号指定的方式计算表达式所需的基本乘法数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值