Advertisement
ShafiulAzim

uva 673

Feb 3rd, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. // freopen("input.txt","r",stdin);
  6. // freopen("output.txt","w",stdout);
  7. stack<char>s;
  8. string str;
  9. int i,n,j,k;
  10. cin>>n;
  11. for(k=1;k<=n;k++)
  12. {
  13. if(k!=1)
  14. getline(cin,str);
  15. else{
  16. getchar();
  17. getline(cin,str);
  18. }
  19. if(str.size()==0)
  20. cout<<"Yes"<<endl;
  21. else if(str[0]==')' || str[0]=='}' || str[0]==']')
  22. cout<<"No"<<endl;
  23. else
  24. {
  25. for(j=0; j<str.size(); j++)
  26. {
  27. if(str[j]=='(')
  28. {
  29. s.push('(');
  30. }
  31. else if(str[j]=='{')
  32. {
  33. s.push('{');
  34. }
  35. else if(str[j]=='[')
  36. {
  37. s.push('[');
  38. }
  39. else if(str[j]==')')
  40. {
  41. if(s.empty()==0 && s.top()== '(' )
  42. s.pop();
  43. else
  44. {
  45. s.push(')');
  46. break;
  47. }
  48. }
  49.  
  50. else if(str[j]=='}')
  51. {
  52. if(s.empty()==0 && s.top()== '{')
  53. s.pop();
  54. else
  55. {
  56. s.push('}');
  57. break;
  58. }
  59. }
  60. else if(str[j]==']')
  61. {
  62. if(s.empty()==0 && s.top()== '[')
  63. s.pop();
  64. else
  65. {
  66. s.push(']');
  67. break;
  68. }
  69. }
  70.  
  71. }
  72. if(s.empty()==1)
  73. cout<<"Yes"<<endl;
  74. else
  75. cout<<"No"<<endl;
  76. while(s.empty()!=1){
  77. s.pop();
  78. }
  79.  
  80. }
  81. }
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement