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

来源:互联网 发布:三级域名申请 编辑:程序博客网 时间:2024/05/29 15:13
#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
0 0
原创粉丝点击