Advertisement
Griwin

Untitled

Nov 21st, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <limits.h>
  7. #include <stdbool.h>
  8.  
  9.  
  10. int main(){
  11. int t,top=-1,i,flag,a0;
  12. char stack[100],temp;
  13. scanf("%d",&t);
  14. for(int a0 = 0; a0 < t; a0++){
  15. char* s = (char *)malloc(10240 * sizeof(char));
  16. scanf("%s",s);
  17. flag=1;
  18. for(i=0;s[i]!='\0';i++)
  19. {
  20. if(s[i] == '('||s[i]=='{'||s[i]=='[')
  21. stack[++top]=s[i];
  22. if(s[i] == ')'||s[i]=='}'||s[i]==']')
  23. {
  24. if(top == -1)
  25. {
  26. flag=0;
  27. break;
  28. }
  29.  
  30. else
  31. {
  32. temp=stack[top--];
  33.  
  34. if((s[i] == ')' &&temp != '(') ||(s[i] == '}' && temp!='{')||(s[i] == ']' && temp!='['))
  35. flag=0;
  36. }
  37. }
  38. }
  39. if(top>=0)
  40. {
  41. while(top >= 0)
  42. temp=stack[top--];
  43. flag=0;
  44. }
  45.  
  46. if(flag == 0)
  47. printf("NO\n");
  48. else
  49. printf("YES\n");
  50. }
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement