这个题自己刚开始写的,受前面那个题的影响开了俩个栈,一个拿来存括号了,然而这个题保证输入合法,
不需要将括号入栈,另外自己刚开始还重重载了矩阵赋值函数,后来发现矩阵竟然不需要重载赋值函数,不知道为什么矩阵不需要
重载赋值函数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cctype>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
#include<algorithm>
#define LL long long
using namespace std;
struct matrix
{
int r,c;
matrix(int r,int c):r(r),c(c){}
matrix() {}
/* matrix operator = (matrix b)
{
this->r=b.r;
this->c=b.c;
return *this;
}*/
};
int main()
{
int n;
cin>>n;
// getchar();
matrix ma[30];
for(int i=0;i<n;i++)
{
char c;
int a,b;
cin>>c>>a>>b;
int cc=c-'A';
ma[cc].r=a;
ma[cc].c=b;
}
getchar();
string s;
while(getline(cin,s))
{
int len=s.length();
if(len==1)
{
cout<<0<<endl;
continue;
}
// stack<char> s1;
stack<matrix> s2;
int sum=0;
int flag=1;
for(int i=0;i<len;i++)
{
// if(s[i]=='(') s1.push(s[i]);
if(isalpha(s[i])) s2.push(ma[s[i]-'A']);
else if(s[i]==')')
{
// s1.pop();
matrix a,b;
// cout<<s2.top().r<<endl;
a=s2.top();
s2.pop();
b=s2.top();
s2.pop();
// cout<<b.r<<' '<<b.c<<' '<<a.r<<' '<<a.c<<endl;
if(b.c!=a.r)
{
flag=0;
break;
}
sum=sum+a.r*b.r*a.c;
s2.push(matrix(b.r,a.c));
}
}
if(flag)
cout<<sum<<endl;
else
cout<<"error"<<endl;
}
return 0;
}