35657

Untitled

Oct 5th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <fstream>
  6.  
  7. bool is_correct(const std::string& str) {
  8.     std::vector<char> vec;
  9.  
  10.     for (int i = 0; i < str.size(); i++) {
  11.         if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
  12.             vec.push_back(str[i]);
  13.         }
  14.         else if (str[i] == ')') {
  15.             if (vec.empty() || vec.back() != '(') {
  16.                 return false;
  17.             }
  18.             vec.pop_back();
  19.         }
  20.         else if (str[i] == ']') {
  21.             if (vec.empty() || vec.back() != '[') {
  22.                 return false;
  23.             }
  24.             vec.pop_back();
  25.         }
  26.         else if (str[i] == '}') {
  27.             if (vec.empty() || vec.back() != '{') {
  28.                 return false;
  29.             }
  30.             vec.pop_back();
  31.         }
  32.     }
  33.  
  34.     if (!vec.empty()) {
  35.         return false;
  36.     }
  37.     return true;
  38. }
  39.  
  40.  
  41. int main() {
  42.  
  43.     std::ifstream fin("input.txt");
  44.  
  45.     std::string str;
  46.  
  47.     fin >> str;
  48.  
  49.     is_correct(str) ? std::cout << "True" : std::cout << "False";
  50. }
Advertisement
Add Comment
Please, Sign In to add comment