mfgnik

Untitled

Jun 27th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4.  
  5. enum class BracketType {
  6.     Round, Square, Curl
  7. };
  8.  
  9. enum class Direction {
  10.     Open, Close
  11. };
  12.  
  13. struct Bracket {
  14.     BracketType type;
  15.     Direction direction;
  16.     explicit Bracket (char charBracket) {
  17.         if (charBracket == '(' or charBracket == '[' or charBracket == '{') {
  18.             this->direction = Direction::Open;
  19.         } else {
  20.             this->direction = Direction::Close;
  21.         }
  22.         if (charBracket == '(' or charBracket == ')') {
  23.             this->type = BracketType::Round;
  24.         } else if (charBracket == '[' or charBracket == ']') {
  25.             this->type = BracketType::Square;
  26.         } else {
  27.             this->type = BracketType::Curl;
  28.         }
  29.     }
  30. };
  31.  
  32.  
  33. int main() {
  34.     std::ios_base::sync_with_stdio(false);
  35.     std::stack<Bracket> brackets;
  36.     std::string line_with_brackets;
  37.     std::cin >> line_with_brackets;
  38.     size_t minimal_correct = 0;
  39.     bool correct = true;
  40.     for (char bracket_char: line_with_brackets) {
  41.         Bracket bracket{bracket_char};
  42.         if (bracket.direction == Direction::Open) {
  43.             brackets.push(bracket);
  44.         } else if (!brackets.empty() && bracket.type == brackets.top().type) {
  45.             brackets.pop();
  46.         } else {
  47.             correct = false;
  48.             break;
  49.         }
  50.         ++minimal_correct;
  51.     }
  52.     if (correct && brackets.empty()) {
  53.         std::cout << "CORRECT";
  54.     } else {
  55.         std::cout << minimal_correct;
  56.     }
  57. }
Add Comment
Please, Sign In to add comment