uva442

#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define eps 1e-8
#define M_PI 3.141592653589793

typedef long long ll;
const ll mod=1000000007;
const int inf=0x7fffffff;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

using namespace std;
struct M{
    int x,y;
}a[150];
stack<M>sta;
int main()
{
   ios::sync_with_stdio(false);
   int n; cin>>n;
   for(int i=0;i<n;i++){
      char c;
      cin>>c;
      int d=c-'A';
      cin>>a[d].x>>a[d].y;
   }
   string s;
   while(cin>>s){
       int flag=0,res=0;
       while(!sta.empty()) sta.pop();
       if(s[0]!='(') cout<<0<<endl;
       else{
         for(int i=0;i<s.size();i++){
            if(isalpha(s[i])) sta.push(a[s[i]-'A']);
            else if(s[i]==')'){
                M t2=sta.top();sta.pop();
                M t1=sta.top();sta.pop();
                //cout<<t1.x<<t1.y<<t2.x<<t2.y<<endl;
                if(t1.y!=t2.x){
                    flag=1;
                    break;
                }
                res+=t1.x*t1.y*t2.y;
                M t={t1.x,t2.y};
                sta.push(t);
            }
         }
         if(flag) cout<<"error"<<endl;
         else cout<<res<<endl;
      }
   }
}


你可能感兴趣的:(uva442)