#include<iostream>
#include<stack>
using namespace std;
struct node
{
char ch;
int a,b;
};
node arr[27];
int main()
{
stack<node> sta;
int num,k=0;
cin>>num;
while(num--)
{
cin>>arr[k].ch>>arr[k].a>>arr[k].b;
k++;
}
char ch;
int sum=0;
cin.get();
while(cin.get(ch))
{
if(ch=='\n')
{
if(!sta.empty())
{
while(!sta.empty())
sta.pop();
cout<<sum<<endl;
}
sum=0;
continue;
}
if(ch=='(')
continue;
if(ch==')')
{
if(sta.size()==1)
{
cout<<sum<<endl;
cin.get();
continue;
}
node t1,t2;
t1=sta.top();
sta.pop();
t2=sta.top();
sta.pop();
if(t2.b!=t1.a)
{
cout<<"error"<<endl;
while(!sta.empty())
sta.pop();
sum=0;
cin.get();
continue;
}
node temp;
temp.a=t2.a;
temp.b=t1.b;
sta.push(temp);
sum+=t2.a*t1.a*t1.b;
}
for(int i=0;i<k;i++)
{
if(arr[i].ch==ch)
{
sta.push(arr[i]);
break;
}
}
}
return 0;
}
zoj1094-Matrix Chain Multiplication
最新推荐文章于 2012-12-10 11:51:23 发布