35657

Untitled

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