aimon1337

Untitled

Mar 20th, 2022 (edited)
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. using std::string;
  5.  
  6. struct Stack
  7. {
  8.     std::queue<char> q1, q2;
  9.     int curr_size;
  10.     Stack()
  11.     {
  12.         curr_size = 0;
  13.     }
  14.     void push(char x)
  15.     {
  16.         q1.push(x);
  17.         curr_size++;
  18.     }
  19.     void pop()
  20.     {
  21.         if (q1.empty())
  22.             return;
  23.         while (q1.size() != 1)
  24.         {
  25.             q2.push(q1.front());
  26.             q1.pop();
  27.         }
  28.         q1.pop();
  29.         curr_size--;
  30.         std::queue<char> q = q1;
  31.         q1 = q2;
  32.         q2 = q;
  33.     }
  34.     int top()
  35.     {
  36.         if (q1.empty())
  37.             return -1;
  38.  
  39.         while (q1.size() != 1) {
  40.             q2.push(q1.front());
  41.             q1.pop();
  42.         }
  43.         char temp = q1.front();
  44.         q1.pop();
  45.         q2.push(temp);
  46.         std::queue<char> q;
  47.         q = q1;
  48.         q1 = q2;
  49.         q2 = q;
  50.         return temp;
  51.     }
  52.     int size()
  53.     {
  54.         return curr_size;
  55.     }
  56.     bool empty()
  57.     {
  58.         return curr_size == 0;
  59.     }
  60. };
  61.  
  62. bool isValid(string s)
  63. {
  64.     Stack st;
  65.     for (char c : s)
  66.     {
  67.         if (st.empty())
  68.         {
  69.             if (c == '(' || c == '[' || c == '{')
  70.             {
  71.                 st.push(c);
  72.             }
  73.             else
  74.             {
  75.                 return false;
  76.             }
  77.         }
  78.         else
  79.         {
  80.             if (c == '(')
  81.             {
  82.                 if (st.top() == '(')
  83.                 {
  84.                     return false;
  85.                 }
  86.                 else
  87.                 {
  88.                     st.push(c);
  89.                 }
  90.             }
  91.             else if (c == ')')
  92.             {
  93.                 if (st.top() != '(')
  94.                 {
  95.                     return false;
  96.                 }
  97.                 else
  98.                 {
  99.                     st.pop();
  100.                 }
  101.             }
  102.             else if (c == '[')
  103.             {
  104.                 if (st.top() == '(' || st.top() == '[')
  105.                 {
  106.                     return false;
  107.                 }
  108.                 else
  109.                 {
  110.                     st.push(c);
  111.                 }
  112.             }
  113.             else if (c == ']')
  114.             {
  115.                 if (st.top() != '[')
  116.                 {
  117.                     return false;
  118.                 }
  119.                 else
  120.                 {
  121.                     st.pop();
  122.                 }
  123.             }
  124.             else if (c == '{')
  125.             {
  126.                 if (st.top() == '(' || st.top() == '[' || st.top() == '{')
  127.                 {
  128.                     return false;
  129.                 }
  130.                 else
  131.                 {
  132.                     st.push(c);
  133.                 }
  134.             }
  135.             else if (c == '}')
  136.             {
  137.                 if (st.top() != '{')
  138.                 {
  139.                     return false;
  140.                 }
  141.                 else
  142.                 {
  143.                     st.pop();
  144.                 }
  145.             }
  146.         }
  147.     }
  148.     if(st.empty())
  149.         return true;
  150.     else return false;
  151. }
  152. int main()
  153. {
  154.     string s;
  155.     std::cin >> s;
  156.     if (isValid(s))
  157.     {
  158.         std::cout << "Expresia este valida.\n";
  159.     }
  160.     else
  161.     {
  162.         std::cout << "Expresia nu este valida.\n";
  163.     }
  164.     return 0;
  165. }
Add Comment
Please, Sign In to add comment