题目链接:UVa442题目
题目大意:
输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数。如果乘法无法进行,输出error。
假定A是m*n矩阵,B是n*p矩阵,那么AB是m*p矩阵,乘法次数为m*n*p。如果A的列数不等于B的行数,则乘法无法进行。
思路:
关键是解析表达式。本题表达式较为简单,可以借助一个栈来完成:遇到字母时压入栈,遇到右括号时出栈并计算,然后结果入栈。
因为输入保证合法,所以括号无需入站。
样例输入:
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))样例输出:
0 0 0 error 10000 error 3500 15000 40500 47500 15125
代码:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
struct Matrix {
int a, b;
Matrix(int a = 0,int b = 0):a(a),b(b){}
}m[26];
stack<Matrix>s;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string name;
cin >> name;
int k = name[0] - 'A';
cin >> m[k].a >> m[k].b;
}
string expr;
while (cin >> expr) {
int len = expr.length();
bool error = false;
int ans = 0;
for (int i = 0; i < len; i++) {
if (isalpha(expr[i])) s.push(m[expr[i] - 'A']);
else if (expr[i] == ')') {
Matrix m2 = s.top(); s.pop();
Matrix m1 = s.top(); s.pop();
if (m1.b != m2.a) { error = true; break; }
ans += m1.a*m1.b*m2.b;
s.push(Matrix(m1.a, m2.b));
}
}
if (error)
printf("error\n");
else
printf("%d\n", ans);
}
system("pause");
return 0;
}