Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cctype>
- #include <iostream>
- #include <string>
- #include <vector>
- class StringParser {
- public:
- void ProcessSymbol(char symbol) {
- if (symbol == '[') {
- repeats_ *= current_number_;
- stack_.push_back(current_number_);
- current_number_ = 0;
- } else if (symbol == ']') {
- repeats_ /= stack_.back();
- stack_.pop_back();
- } else if (std::isdigit(symbol)) {
- current_number_ *= 10;
- current_number_ += symbol - '0';
- } else {
- length_ += repeats_;
- }
- }
- int64_t GetLength() const {
- return length_;
- }
- private:
- std::vector<int64_t> stack_;
- int64_t current_number_ = 0, length_ = 0, repeats_ = 1;
- };
- int main() {
- std::string string;
- std::cin >> string;
- StringParser parser;
- for (auto symbol : string) {
- parser.ProcessSymbol(symbol);
- }
- std::cout << parser.GetLength() << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement