SHARE
TWEET

Untitled

a guest Dec 19th, 2017 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. int main()
  5. {
  6.     std::stack<char> brackets;
  7.     std::string input;
  8.  
  9.     std::getline(std::cin, input);
  10.  
  11.     bool isValid = true;
  12.  
  13.     for (char i : input)
  14.     {
  15.         switch (i)
  16.         {
  17.             case '(':
  18.                 brackets.push(')');
  19.                 break;
  20.             case '{':
  21.                 brackets.push('}');
  22.                 break;
  23.             case '[':
  24.                 brackets.push(']');
  25.                 break;
  26.             case ')':
  27.             case '}':
  28.             case ']':
  29.                 if (brackets.empty() || brackets.top() != i)
  30.                 {
  31.                     isValid = false;
  32.                 }
  33.                 else
  34.                 {
  35.                     brackets.pop();
  36.                 }
  37.                 break;
  38.             default:
  39.                 break;
  40.         }
  41.  
  42.         if (!isValid)
  43.         {
  44.             break;
  45.         }
  46.     }
  47.  
  48.     if (isValid && brackets.empty())
  49.     {
  50.         std::cout << "Valid" << std::endl;
  51.     }
  52.     else
  53.     {
  54.         std::cout << "Invalid" << std::endl;
  55.     }
  56.  
  57.     return 0;
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top