fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int t;
  6. cin>>t;
  7. while(t--)
  8. {
  9. string str;
  10. cin>>str;
  11. stack<char> st;
  12.  
  13. for(int i=0;i<str.length();i++)
  14. {
  15. if(str[i]=='{' || str[i]=='(' || str[i]=='[')
  16. st.push(str[i]);
  17. else if(!st.empty()){
  18.  
  19. if(str[i]=='}' && st.top()=='{' )
  20. st.pop();
  21. else if(str[i]==')' && st.top()=='(' )
  22. st.pop();
  23. else if(str[i]==']' && st.top() == '[' )
  24. st.pop();
  25. }
  26. else if(st.empty())
  27. {
  28. cout<<"not balanced\n";
  29. break;
  30. }
  31.  
  32. }
  33. if(st.empty())
  34. cout<<"balanced\n";
  35. else cout<<"not balanced\n";
  36.  
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 16064KB
stdin
6
{([])}
()
()[]
((((
))}}}}}}]]]]
{}{})))
stdout
balanced
balanced
balanced
not balanced
not balanced
balanced
not balanced
balanced