水题。



#include<iostream>
#include<string>
#include<stack>
#include<cctype>
using namespace std;

struct Matrix {
    int a, b;
    Matrix (int a = 0, int b = 0) : a(a), b(b) {}            //初始化
} m[26];

int main() {
    int n;
    while(cin >> n) {
        for(int i = 0; i < n; i++) {
            string M;
            cin >> M;
            cin >> m[M[0] - 'A'].a >> m[M[0] - 'A'].b;
        }
        string cmd;
        while(cin >> cmd) {
            stack<Matrix> s;
            int ant = 0, len = cmd.size();
            bool error = false;
            for(int i = 0; i < len; i++) {
                if(isalpha(cmd[i])) s.push(m[cmd[i] - 'A']);
                else if(cmd[i] == ')') {
                    Matrix m2 = s.top(); s.pop();
                    Matrix m1 = s.top(); s.pop();
                    if(m1.b != m2.a) {
                        error = true;
                        break;
                    } else {
                        ant += m1.a * m1.b * m2.b;
                        s.push(Matrix(m1.a, m2.b));
                    }
                }
            }
            error ? cout << "error" << endl : cout << ant << endl;
        }
    }
    return 0;
}