fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Node
  6. {
  7. char Data;
  8. struct Node * next;
  9. }Node;
  10.  
  11. typedef struct Linkedlist
  12. {
  13. Node * cur;
  14. int sizeOfData;
  15. }Stack;
  16.  
  17. void Init(Stack * pstack)//초기화
  18. {
  19. pstack->cur = NULL;
  20. pstack->sizeOfData = 0;
  21. }
  22.  
  23. void Push(Stack * pstack, char pdata)//푸쉬
  24. {
  25. Node * newNode = (Node *)malloc(sizeof(Node));
  26.  
  27. newNode->Data = pdata;
  28. newNode->next = pstack->cur;
  29.  
  30. pstack->cur = newNode;
  31. (pstack->sizeOfData)++;
  32. }
  33.  
  34. void Pop(Stack * pstack)//팝
  35. {
  36. char rdata;
  37. Node * rNode = pstack->cur;
  38.  
  39. rdata = pstack->cur->Data;
  40. pstack->cur = pstack->cur->next;
  41.  
  42. free(rNode);
  43. (pstack->sizeOfData)--;
  44. }
  45.  
  46. int Top(Stack * pstack)
  47. {
  48. return pstack->cur->Data;
  49. }
  50.  
  51. int VPS(Stack * pstack, char * sentence)
  52. {
  53. int fin = 0;
  54. Init(pstack);
  55. for(int i =0;i<50;i++)
  56. {
  57. if(sentence[i] == '(')
  58. {
  59. Push(pstack, '(');
  60. fin++;
  61. }
  62. else if(sentence[i] == ')')
  63. {
  64. if(pstack->sizeOfData == 0)
  65. return 0;
  66. Push(pstack, ')');
  67. Pop(pstack);
  68. Pop(pstack);
  69. fin --;
  70. }
  71. }
  72.  
  73. if(pstack->sizeOfData ==0 && fin ==0)
  74. {
  75. return 1;
  76. }
  77. else
  78. {
  79. return 0;
  80. }
  81. }
  82.  
  83. int main()
  84. {
  85. Stack stack;
  86. int orderNum;
  87. char order[10000];
  88.  
  89. Init(&stack);
  90.  
  91. scanf("%d",&orderNum);
  92.  
  93. for(int i = 0; i < orderNum;i++)
  94. {
  95. scanf(" %[^\n]",order);
  96. if(VPS(&stack, order))
  97. {
  98. printf("YES");
  99. }
  100. else
  101. {
  102. printf("NO");
  103. }
  104. printf("\n");
  105. }
  106. return 0;
  107. }
Success #stdin #stdout 0s 9424KB
stdin
2
)))
(
stdout
NO
YES