Advertisement
mfgnik

Untitled

Jun 6th, 2020
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <cctype>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6.  
  7. class StringParser {
  8. public:
  9.     void ProcessSymbol(char symbol) {
  10.         if (symbol == '[') {
  11.             repeats_ *= current_number_;
  12.             stack_.push_back(current_number_);
  13.             current_number_ = 0;
  14.         } else if (symbol == ']') {
  15.             repeats_ /= stack_.back();
  16.             stack_.pop_back();
  17.         } else if (std::isdigit(symbol)) {
  18.             current_number_ *= 10;
  19.             current_number_ += symbol - '0';
  20.         } else {
  21.             length_ += repeats_;
  22.         }
  23.     }
  24.  
  25.     int64_t GetLength() const {
  26.         return length_;
  27.     }
  28.  
  29. private:
  30.     std::vector<int64_t> stack_;
  31.     int64_t current_number_ = 0, length_ = 0, repeats_ = 1;
  32. };
  33.  
  34. int main() {
  35.     std::string string;
  36.     std::cin >> string;
  37.     StringParser parser;
  38.     for (auto symbol : string) {
  39.         parser.ProcessSymbol(symbol);
  40.     }
  41.     std::cout << parser.GetLength() << "\n";
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement