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
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 }
Line = Expression
Expression = Matrix | “(” Expression Expression “)”
Matrix = “A” | “B” | “C” | … | “X” | “Y” | “Z”
Output
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.
Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))
Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125

这道题就是考察数据结构栈的使用,以及矩阵乘法的性质。
其中,如有矩阵A(m*n)、B(n*p)矩阵的乘法运算次数times为m*n*p。知道了这个,就好办了。用STL的stack,遇见’)’就出栈运算就可以了。代码如下:

#include <iostream>
#include <stack>
#include <string>
#include <cctype>

using namespace std;

const int MAX = 30;
struct Matrix
{
    int row, col;
    Matrix(int row, int col):row(row),col(col){} //带参构造函数
    Matrix() :row(0), col(0){}                   //默认构造函数
};


int main()
{
    int t{ 0 };
    Matrix x[MAX];//存放所有的矩阵行列数信息
    cin >> t;
    stack<Matrix>s;
    for (int i = 0; i < t; i++) {
        char matrixName;
        cin >> matrixName;
        cin >> x[matrixName - 'A'].row >> x[matrixName - 'A'].col;
     }
    string expression;
    while (cin >> expression) {
        bool error{ false };
        int times  { 0 };
        auto len = expression.length();
        for (string::size_type i = 0; i < len; i++) {
            if (isalpha(expression[i])) s.push(x[expression[i] - 'A']);
            else if (expression[i] == ')') {//遇见')就出栈
                Matrix m2 = s.top();  //靠后的矩阵,如有矩阵A、B,则m2是B,m1是A
                s.pop();
                Matrix m1 = s.top();
                s.pop();
                if (m1.col != m2.row) {//A的列数不等于B的行数,说明不符合矩阵乘法规则
                    error = true;
                    break;
                }
                times = times + m1.row*m1.col*m2.col;//矩阵乘法运算次数等于矩阵A的行数乘以列数再乘以矩阵B的列数
                s.push(Matrix(m1.row, m2.col));//将运算得到的新矩阵入栈
            }
        }
        if (error)cout << "error" << endl;
        else cout << times << endl;
    }
    return 0;
}