UVa442

https://vjudge.net/problem/UVA-442

 

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 30;
int n;

struct Matrix{
    int row, col;
    Matrix(int x, int y):row(x), col(y){}
    Matrix(){}
};
Matrix matrix[maxn];

int main()
{
    while (cin >> n)
    {
        for (int i = 1; i <= n; i++)
        {
            char ch; int row, col;
            cin >> ch >> row >> col;
            matrix[ch-'A'] = Matrix(row, col);
        }
        char cmd[maxn];
        while (cin >> cmd)
        {
            stack<Matrix> s;
            int cal_t = 0;//计算次数
            bool flag = false;  //正误标记
            int len = strlen(cmd);
            for (int i = 0; i < len; i++)
            {
                if (cmd[i] == ')')
                {
                    Matrix m1 = s.top(); s.pop();
                    Matrix m2 = s.top(); s.pop();
                    if (m1.row == m2.col)//注意顺序
                    {
                        cal_t += m2.row*m2.col*m1.col;
                        s.push(Matrix(m2.row, m1.col));//注意顺序
                    }
                    else
                    {
                        flag = true;
                        break;
                    }
                }
                else if (cmd[i] >= 'A' && cmd[i] <= 'Z')
                    s.push(matrix[cmd[i] - 'A']);
            }
            if (flag)
                cout << "error" << endl;
            else
                cout << cal_t << endl;
        }

    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值