Advertisement
gabrielcabezas

Untitled

Apr 1st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. stack <char> s;
  4. string c;
  5. int main()
  6. {
  7. int n,ok;
  8. scanf("%d",&n);
  9. while (n--)
  10. {
  11. ok=1;
  12. while (!s.empty())
  13. s.pop();
  14. cin >> c;
  15. for (int i=0;i<c.size();i++)
  16. {
  17. if (c[i]=='(' || c[i]=='[')
  18. s.push(c[i]);
  19. else if ((s.size()!=0 && s.top()=='('&& c[i]==')') || (s.size()!=0 && s.top()=='['&& c[i]==']'))
  20. s.pop();
  21. else if (s.size()==0 && c[i]==')' || s.size()==0 && c[i]==']')
  22. ok=0;
  23. else if ((s.size()!=0 && s.top()=='(' && c[i]==']')|| (s.size()!=0 && s.top()=='['&& c[i]==')'))
  24. ok=0;
  25. }
  26. if (s.size())
  27. ok=0;
  28. if (ok)
  29. printf("Yes\n");
  30. else
  31. printf("No\n");
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement