fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool is_balanced(string expression) {
  4. int j=expression.size()-1;
  5. for(int i=0; i<expression.size()/2; i++){
  6. if((expression[i]=='{' && expression[j] =='}') || (expression[i]=='[' && expression[j] ==']') || expression[i]=='(' && expression[j] ==')')
  7. j--;
  8. else
  9. return false;
  10. }
  11. return true;
  12. }
  13.  
  14. int main(){
  15. int t;
  16. cin >> t;
  17. for(int a0 = 0; a0 < t; a0++){
  18. string expression;
  19. cin >> expression;
  20. bool answer=is_balanced(expression);
  21. if(answer==true)
  22. cout << "YES\n";
  23. else cout << "NO\n";
  24. }
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 15232KB
stdin
3
{[()]}
{[(])}
{{[[(())]]}}
stdout
YES
NO
YES