UVA 442

#include<iostream>
#include<stack>
#include<cctype>
#include<map>
#include<string>
using namespace std;
struct  matrix
{
    int x,y;
};
int main()
{
    int n;
    cin>>n;
    char ch;
    int i,x,y;
    matrix   ma;
    map<int,matrix> m;
    for(i=0;i<n;i++)
    {
        cin>>ch;
        cin.ignore();
        cin>>x;
        cin.ignore();
        cin>>y;
        cin.ignore();
        ma.x=x;
        ma.y=y;
        m.insert(make_pair(ch-'A',ma));
    }
    string temp;
    int flag2=0;
    while(getline(cin,temp))
    {
        if(temp.length()==1)
            cout<<0<<endl;
        else
        {
            stack<int> st;
            int j,sum=0,flag=1;
            for(j=0;j<temp.length();j++)
            {
                if(temp[j]=='(')
                    st.push(30);
                else if(temp[j]!='('&&temp[j]!=')')
                    st.push(temp[j]-'A');
                else 
                {
                    int a,b;
                    a=st.top();
                    st.pop();
                    b=st.top();
                    st.pop();
                    if(m[a].x!=m[b].y)
                    {
                        flag=0;
                        break;
                    }
                    else
                    {
                        sum+=m[a].y*m[b].x*m[a].x;
                        st.pop();
                        matrix  temp_ma;
                        temp_ma.x=m[b].x;
                        temp_ma.y=m[a].y;
                        st.push(flag2+100);
                        m.insert(make_pair(flag2+100,temp_ma));
                        flag2++;
                    }
                }
            }
            if(flag)
                cout<<sum<<endl;
            else
                cout<<"error"<<endl;
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/maqiang/archive/2012/05/05/2484455.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值