Advertisement
ShafiulAzim

Untitled

Feb 2nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 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(i=1; i<=n; i++)
  12. {
  13. getline(cin,str);
  14. if(str[0]==')' || str[0]=='}' || str[0]==']')
  15. cout<<"No"<<endl;
  16. else
  17. {
  18. for(j=0; j<str.size(); j++)
  19. {
  20. if(str[j]=='(')
  21. {
  22. s.push('(');
  23. }
  24. else if(str[j]=='{')
  25. {
  26. s.push('{');
  27. }
  28. else if(str[j]=='[')
  29. {
  30. s.push('[');
  31. }
  32. else if(str[j]==')')
  33. {
  34. if(s.empty()==0 && s.top()== '(' )
  35. s.pop();
  36. else
  37. {
  38. s.push(')');
  39. break;
  40. }
  41. }
  42.  
  43. else if(str[j]=='}')
  44. {
  45. if(s.empty()==0 && s.top()== '{')
  46. s.pop();
  47. else
  48. {
  49. s.push('}');
  50. break;
  51. }
  52. }
  53. else if(str[j]==']')
  54. {
  55. if(s.empty()==0 && s.top()== '[')
  56. s.pop();
  57. else
  58. {
  59. s.push(']');
  60. break;
  61. }
  62. }
  63.  
  64. }
  65. if(s.empty()==1)
  66. cout<<"Yes"<<endl;
  67. else
  68. cout<<"No"<<endl;
  69. while(s.empty()!=1){
  70. s.pop();
  71. }
  72.  
  73. }
  74. }
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement