fork(2) download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <locale.h>
  6.  
  7.  
  8. typedef struct _INFO
  9. {
  10. char s;
  11. struct _INFO* next;
  12. }INFO;
  13. typedef INFO* STACK;
  14.  
  15. int Push(STACK* stack, char s)
  16. {
  17. INFO* temp = (INFO*)malloc(sizeof(INFO));
  18. temp->s = s;
  19. if (!temp) return 1;
  20. if (!*stack)
  21. temp->next = NULL;
  22. else
  23. temp->next = *stack;
  24. *stack = temp;
  25. return 0;
  26. }
  27. int Pop(STACK* stack, char* s)
  28. {
  29. if (!*stack) return 1;
  30. INFO* temp = *stack;
  31. *s = temp->s;
  32. *stack = temp->next;
  33. free(temp);
  34. return 0;
  35.  
  36. }
  37.  
  38. int main (void)
  39. {
  40. // freopen("input.txt", "r", stdin);
  41. // freopen("output.txt", "w", stdout);
  42. char str[1024];
  43.  
  44.  
  45. char *estr;
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. while (1)
  55. {
  56. estr = fgets (str,sizeof(str),stdin);
  57. if (estr == NULL)
  58. {
  59. if ( feof (stdin) != 0)
  60. {
  61.  
  62. break;
  63. }
  64.  
  65.  
  66. }
  67.  
  68. STACK head = NULL;
  69. bool flag = true;
  70. char * sp = & (str[0]);
  71.  
  72. char s, s_1;
  73. while ( ( * sp ) != '\n' )
  74. {
  75. char s = ( * sp ) ;
  76. if (s == '(' || s == '{' || s == '[')
  77. {
  78. Push(&head, s);
  79. }
  80. if (s == ')' || s == '}' || s == ']')
  81. {
  82. if (!head) { flag = false; break; }
  83.  
  84. if (head->s == '(' && s == ')') Pop(&head, &s_1);
  85. else if (head->s == '[' && s == ']') Pop(&head, &s_1);
  86. else if (head->s == '{' && s == '}') Pop(&head, &s_1);
  87.  
  88. }
  89. ++ sp ;
  90. }
  91. if (!head && flag)
  92. printf("YES\n");
  93. else
  94. printf("NO\n");
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101. return 0;
  102. }
  103.  
  104.  
Success #stdin #stdout 0s 4432KB
stdin
([abc]{def})
)[abc]{def}(                                
{}[]()
stdout
YES
NO
YES