#include<iostream>
#include<stack>
#include<string>
using namespace std;
struct Matrix{
int a,b;
Matrix(int a=0,int b=0):a(a),b(b){}
} mats[26];
int main()
{
int N;
cin>>N;
for(int i=0;i<N;i++)
{
char num;
cin>>num;
int k=num-'A';
cin>>mats[k].a>>mats[k].b;
}
string expr;
while(cin>>expr)
{
stack<Matrix> s;
int i=0,N=expr.size(),mul=0;
bool ok=true;
while(i<N)
{
if(expr[i]!='(')
{
if(expr[i]==')')
{
Matrix y=s.top();
s.pop();
Matrix x=s.top();
s.pop();
if(x.b!=y.a){cout<<"error"<<endl;ok=false;break;}
mul+=x.a*x.b*y.b;
s.push(Matrix(x.a,y.b));
}
else
s.push(mats[expr[i]-'A']);
}
i++;
}
if(ok)cout<<mul<<endl;
}
return 0;
}
442 - Matrix Chain Multiplication
最新推荐文章于 2022-07-13 22:21:57 发布