Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <string>
- enum class BracketType {
- Round, Square, Curl
- };
- enum class Direction {
- Open, Close
- };
- struct Bracket {
- BracketType type;
- Direction direction;
- explicit Bracket (char charBracket) {
- if (charBracket == '(' or charBracket == '[' or charBracket == '{') {
- this->direction = Direction::Open;
- } else {
- this->direction = Direction::Close;
- }
- if (charBracket == '(' or charBracket == ')') {
- this->type = BracketType::Round;
- } else if (charBracket == '[' or charBracket == ']') {
- this->type = BracketType::Square;
- } else {
- this->type = BracketType::Curl;
- }
- }
- };
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::stack<Bracket> brackets;
- std::string line_with_brackets;
- std::cin >> line_with_brackets;
- size_t minimal_correct = 0;
- bool correct = true;
- for (char bracket_char: line_with_brackets) {
- Bracket bracket{bracket_char};
- if (bracket.direction == Direction::Open) {
- brackets.push(bracket);
- } else if (!brackets.empty() && bracket.type == brackets.top().type) {
- brackets.pop();
- } else {
- correct = false;
- break;
- }
- ++minimal_correct;
- }
- if (correct && brackets.empty()) {
- std::cout << "CORRECT";
- } else {
- std::cout << minimal_correct;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment