Advertisement
_ash__

Untitled

Dec 22nd, 2020
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. class Solution {
  2. public:    
  3.     bool isDigit(char ch) {
  4.         return ch >= '0' && ch <= '9';
  5.     }
  6.     bool isCapital(char ch) {
  7.         return (ch >= 'A' && ch <= 'Z');
  8.     }
  9.     bool isSmall(char ch) {
  10.         return ch >= 'a' && ch <= 'z';
  11.     }
  12.     string toString(int x) {
  13.         if(x == 0) return "0";
  14.         string s;
  15.         while(x) {
  16.             s.push_back( (x%10) + '0');
  17.             x /= 10;
  18.         }
  19.         reverse(s.begin(), s.end());
  20.         return s;
  21.     }
  22.    
  23.     void Merge(unordered_map<string,int> &a, unordered_map<string,int> &b) {
  24.         for(auto itr : b) {
  25.             a[itr.first] += itr.second;
  26.         }
  27.     }
  28.    
  29.     void multiply(stack<unordered_map<string,int>> &stk, int mul) {
  30.         for(auto &itr : stk.top()) {
  31.             itr.second *= mul;
  32.         }
  33.     }
  34.    
  35.     string countOfAtoms(string formula) {
  36.         formula = "(" + formula + ")";
  37.         stack<unordered_map<string,int>> stk;
  38.         int n = formula.size();
  39.         for(int i = 0; i < n; i++) {
  40.             if(formula[i] == ')') {
  41.                 auto t0 = stk.top();
  42.                 stk.pop();
  43.                 while(!stk.top().empty()) {
  44.                     auto t1 = stk.top();
  45.                     stk.pop();
  46.                     Merge(t0, t1);
  47.                 }
  48.                 stk.pop();
  49.                 stk.push(t0);
  50.             }
  51.             else if(formula[i] == '(') {
  52.                 stk.push(unordered_map<string,int>());
  53.             }
  54.             else if(isCapital(formula[i])) {
  55.                 string symbol;
  56.                 symbol.push_back(formula[i]);
  57.                 ++i;
  58.                 while(isSmall(formula[i])) {
  59.                     symbol.push_back(formula[i]);
  60.                     ++i;
  61.                 }
  62.                 --i;
  63.                 unordered_map<string,int> t;
  64.                 t[symbol] = 1;
  65.                 stk.push(t);
  66.             }
  67.             else if(isDigit(formula[i])) {
  68.                 int number = 0;
  69.                 while(isDigit(formula[i])) {
  70.                     number = 10*number + formula[i]-'0';
  71.                     ++i;
  72.                 }
  73.                 --i;
  74.                 multiply(stk, number);
  75.             }
  76.         }
  77.         vector<pair<string,int>> res;
  78.         for(auto itr : stk.top()) {
  79.             res.push_back({itr.first, itr.second});
  80.         }
  81.         sort(res.begin(), res.end());
  82.         string ans;
  83.         for(int i = 0; i < res.size(); i++) {
  84.             ans += res[i].first;
  85.             if(res[i].second > 1) ans += toString(res[i].second);
  86.         }
  87.         return ans;
  88.     }
  89. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement