uva 442

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stack>

using namespace std;
const int maxn = 100 + 50;
struct Matrix
{
	int rows;
	int cols;
};
Matrix mat[26];
int n, rows, cols;
char ch;
char str[maxn];
stack<Matrix> matStack;
long long int res = 0, temp = 0;
bool flag = true;

int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);

	scanf("%d", &n);
	getchar();
	for (int i = 0; i < n; i++)
	{
		scanf("%c %d %d", &ch, &rows, &cols);
		mat[ch - 'A'].cols = cols;
		mat[ch - 'A'].rows = rows;
		getchar();
	}

	while (EOF != scanf("%s", str))
	{
		res = 0;
		flag = true;
		int length = strlen(str);
		while (!matStack.empty())
			matStack.pop();

		for (int i = 0; i < length; i++)
		{
			if ('(' == str[i])
				continue;

			if (')' != str[i])
				matStack.push(mat[str[i] - 'A']);
			else
			{
				Matrix a = matStack.top();
				matStack.pop();
				Matrix b = matStack.top();
				matStack.pop();

				if (b.cols != a.rows)
				{
					flag = false;
					break;
				}
				res += b.rows * b.cols * a.cols;
				Matrix nm;
				nm.rows = b.rows;
				nm.cols = a.cols;
				matStack.push(nm);
			}
		}
		if (!flag)
			printf("error\n");
		else
			printf("%d\n", res);
	}//while scanf
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值