Advertisement
Saleh127

UVA 442 / Stack - Matrix Mal idea

Apr 8th, 2022
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. /***
  2.  created: 2022-04-09-03.10.55
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12.  
  13.  
  14. int main()
  15. {
  16.     ios_base::sync_with_stdio(0);
  17.     cin.tie(0);
  18.     cout.tie(0);
  19.  
  20.  
  21.  
  22.     ll n,m,i,j,k,l;
  23.  
  24.     char c;
  25.  
  26.     string a;
  27.  
  28.     cin>>n;
  29.  
  30.     map<char,pair<ll,ll>>x;
  31.  
  32.     for(i=0;i<n;i++)
  33.     {
  34.         cin>>c>>j>>k;
  35.  
  36.         x[c]={j,k};
  37.     }
  38.  
  39.  
  40.     while(cin>>a)
  41.     {
  42.         stack<pair<ll,ll>>p;
  43.  
  44.         ll ans=0;
  45.         l=0;
  46.  
  47.         if(a.size()==1)
  48.         {
  49.             cout<<0<<nl;
  50.         }
  51.         else
  52.         {
  53.             for(i=0;i<a.size();i++)
  54.             {
  55.                 if(isalpha(a[i]))
  56.                 {
  57.                     auto d=x[a[i]];
  58.  
  59.                     p.push(d);
  60.                 }
  61.                 else if(a[i]==')')
  62.                 {
  63.                     auto d2=p.top();
  64.                     p.pop();
  65.  
  66.                     auto d1=p.top();
  67.                     p.pop();
  68.  
  69.                     if(d1.second!=d2.first)
  70.                     {
  71.                         ///cout<<d1.second<<" "<<d2.first<<nl;
  72.  
  73.                         cout<<"error"<<nl;
  74.                         l=1;
  75.                         break;
  76.                     }
  77.                     else
  78.                     {
  79.                         ans+=(d1.first*d1.second*d2.second);
  80.                         p.push({d1.first,d2.second});
  81.                     }
  82.                 }
  83.             }
  84.  
  85.             if(!l) cout<<ans<<nl;
  86.         }
  87.     }
  88.  
  89.     get_lost_idiot;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement