您的位置:首页 > 大数据 > 人工智能

UVA 442(p141)----Matrix Chain Multiplication

2016-02-25 22:57 676 查看
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
struct point
{
int a,b;
point(int x=0,int y=0):a(x),b(y) {}
};
int n;
point m[30];
stack<point> s;
int main()
{
cin>>n;
for(int i=0; i<n; i++)
{
char a;
int x,y;
cin>>a>>x>>y;
int tmp=a-'A';
m[tmp].a=x;
m[tmp].b=y;
}
string st;
while(cin>>st)
{
int l=st.length();
int flag=0,ans=0;
for(int i=0; i<l; i++)
{
if(isalpha(st[i]))
s.push(m[st[i]-'A']);
else if(st[i]==')')
{
point x=s.top();
s.pop();
point y=s.top();
s.pop();
if(y.b!=x.a)
{
flag=1;
break;
}
else
{
ans+=y.a*y.b*x.b;
s.push(point(y.a,x.b));
}
}
}
if(flag) cout<<"error"<<endl;
else cout<<ans<<endl;
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=383
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: