UVA442MatrixChainMultiplication

来源:互联网 发布:2017年python饱和了 编辑:程序博客网 时间:2024/06/17 11:28
//UVA442MatrixChainMultiplication#include<cstdio>#include<cstring>#include<iostream>#include<stack>#include<algorithm>using namespace std;const int MAXN = 30;struct Matrix {int a;//rowint b;//col}M[MAXN];int main() {int n;scanf("%d", &n); string tmp;for(int i = 0; i < n; i++) {    cin >> tmp;    int tag = tmp[0] - 'A';    int x, y;cin >> x >> y;M[tag].a = x; M[tag].b = y;}//inputstack<Matrix> multi;while(cin >> tmp) {int len = tmp.length();int ans = 0;bool flag = true;for(int i = 0; i < len; i++) {    if(isalpha(tmp[i])) multi.push(M[tmp[i] - 'A']);else if(tmp[i] == ')') {struct Matrix t1 = multi.top(); multi.pop();struct Matrix t2 = multi.top(); multi.pop();if(t1.a == t2.b) {    ans += t2.b * t1.b * t2.a;        struct Matrix t3;    t3.a = t2.a; t3.b = t1.b;    multi.push(t3);          }        else {        //printf("t1.a = %d, t2.b = %d\n", t1.a, t2.b);        flag = false; break;}}}if(flag) printf("%d\n", ans);else printf("error\n");}return 0;}/*9A 50 10B 10 20C 20 5D 30 35E 35 15F 15 5G 5 10H 10 20I 20 25ABC(AA)(AB)(AC)(A(BC))((AB)C)(((((DE)F)G)H)I)(D(E(F(G(HI)))))((D(EF))((GH)I))*/