_Black_Panther_

673 - Parentheses Balance

Mar 6th, 2021 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. /***
  2.  
  3. Solved by: Mohammad Sabbir Ahmed
  4. Problem name: 673 - Parentheses Balance
  5. Problem link: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&category=0&problem=614&mosmsg=Submission+received+with+ID+26160948
  6.  
  7.  
  8. ***/
  9. #include <bits/stdc++.h>
  10. //#include <stdio.h>
  11. //#include <string.h>
  12. //#include <math.h>
  13. //#include <stdlib.h>
  14. //#include <ctype.h>
  15. //#include <string.h>
  16. //
  17. //#include <algorithm>
  18. //#include <iostream>
  19. //#include <vector>
  20. //#include <map>
  21. //#include <set>
  22. //#include <string>
  23. //#include <sstream>
  24. //#include <queue>
  25. //#include <list>
  26. //#include <string>
  27. //#include <stack>
  28.  
  29. #define ll long long
  30. #define smallestnum 1e18
  31.  
  32. using namespace std;
  33.  
  34.  
  35.  
  36. void solve()
  37. {
  38.     //ll n;
  39.     //char str[130];
  40.     string str;
  41.  
  42.     //scanf("%s", str);
  43.     //cin >> str;
  44.     getline(cin,str);
  45.     //cout << "String: " << str << endl;
  46.     ll len = 0;
  47.  
  48.     len = str.size();
  49.     //cout << "Len: " << len << endl;
  50.     if(len==0)
  51.     {
  52.         printf("Yes\n");
  53.         return;
  54.     }
  55.  
  56.  
  57.  
  58.     stack<char> st;
  59.     for(int i=0; i<len; i++)
  60.     {
  61.         //printf("Asci of %c: %d\n",  str[i]);
  62.         if(str[i] == '(' || str[i] == '[')
  63.         {
  64.             st.push(str[i]);
  65.             //cout << "Empty: " << st.empty() << endl;;
  66.         }
  67.         else
  68.         {
  69.             //cout << "Emp: " << st.empty() << endl;
  70.             if( !st.empty() && str[i] - st.top() == 2 && str[i] == ']'){
  71.                 st.pop();
  72.             }
  73.             else if( !st.empty() && str[i] - st.top() == 1 && str[i] == ')'){
  74.                 st.pop();
  75.             }
  76.             else if(i<len){
  77.                 printf("No\n");
  78.                 return;
  79.             }
  80.         }
  81.     }
  82.     if(st.empty())
  83.         printf("Yes\n");
  84.  
  85.     else  printf("No\n");
  86.  
  87.     return;
  88. }
  89. void tscan()
  90. {
  91.     int t;
  92.     //scanf("d", &t);
  93.     cin >> t;
  94.     getchar();
  95.     while(t--)
  96.     {
  97.         //cout << "Case: " << t  << endl;
  98.         solve();
  99.     }
  100. }
  101.  
  102. int main()
  103. {
  104.  
  105.  
  106. //    freopen("in.txt", "r", stdin);
  107. //    freopen("out.txt", "w", stdout);
  108.     //ios_base::sync_with_stdio(0);
  109.     //cin.tie(0);
  110.  
  111.  
  112.     tscan();
  113.  
  114.     //cout << '[' - ']';
  115.     return 0;
  116. }
  117.  
  118.  
  119.  
Add Comment
Please, Sign In to add comment