#include <iostream>
#include <stack>
#include <cstdio>
using namespace std;
struct node
{
int r,c;
}matrix[200];
char s[1000];
int main()
{
int n,i,sum;
char ch;
scanf("%d",&n);
node temp,temp2;
bool b;
//读入数据
while(n--)
{
getchar();
ch = getchar();
scanf("%d %d",&matrix[ch].r,&matrix[ch].c);
}
//开始计算处理
while(~scanf("%s", s))
{
stack<char> s1;
stack<node> s2;
sum = 0;
b = true;
for(i = 0;s[i]!='\0';i++)
{
if(s[i]=='(')
s1.push(s[i]);
else if(s[i]==')')
{
s1.pop();//弹出#
s1.pop();//继续弹出#
s1.pop();//弹出(
temp = s2.top();
s2.pop();
temp2 = s2.top();
s2.pop();
if(temp2.c!=temp.r)
{
b = false;//如果两个矩阵不能相乘,则置b为假,并退出循环
break;
}
sum+=temp2.r*temp2.c*temp.c;
temp2.c = temp.c;
s2.push(temp2);
s1.push('#');
}
else
{
s1.push('#');
s2.push(matrix[s[i]]);
}
}
if(b)
printf("%d\n",sum);
else
printf("error\n");
}
return 0;
}
UVaOJ
最新推荐文章于 2016-09-06 11:04:40 发布