fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX_STACK_SIZE 200
  6.  
  7. typedef char element;
  8.  
  9. typedef struct StackType {
  10. element data[MAX_STACK_SIZE];
  11. int top;
  12. }StackType;
  13.  
  14. void init_stack(StackType* s) {
  15. s->top = -1;
  16. }
  17.  
  18. int is_empty(StackType* s) {
  19. return (s->top == -1);
  20. }
  21.  
  22. // top은 현재 최고 높이 요소의 인덱스, 스택이 꽉차면 스택 크기 - 1
  23. int is_full(StackType* s) {
  24. return (s->top == (MAX_STACK_SIZE - 1));
  25. }
  26.  
  27. void push(StackType* s, element item) {
  28. if (is_full(s)) {
  29. fprintf(stderr, "스택 포화 에러\n");
  30. return;
  31. }
  32.  
  33. else
  34. s->data[++(s->top)] = item;
  35. }
  36.  
  37. char pop(StackType* s) {
  38. if (is_empty(s)) {
  39. fprintf(stderr, "스택 공백 에러\n");
  40. return;
  41. }
  42.  
  43. else
  44. return s->data[(s->top)--];
  45. }
  46.  
  47. int check_valid(char* string) {
  48. StackType s;
  49. char ch1, ch2;
  50. int n = strlen(string);
  51. init_stack(&s);
  52.  
  53. for (int i = 0; i < n; i++) {
  54. ch1 = string[i];
  55. if (ch1 == '(' || ch1 == '[') {
  56. push(&s, ch1);
  57. }
  58.  
  59. else if (ch1 == ')' || ch1 == ']') {
  60. if (is_empty(&s)) return 0;
  61.  
  62. else {
  63. ch2 = pop(&s);
  64. if (!((ch1 == ')' && ch2 == '(') || (ch1 == ']' && ch2 == '['))) {
  65. return 0;
  66. }
  67. }
  68. }
  69. }
  70. if (!is_empty(&s)) return 0;
  71. return 1;
  72. }
  73.  
  74. int main() {
  75. char string[MAX_STACK_SIZE];
  76. int num = 0;
  77. scanf("%[^\n]s", string);
  78.  
  79. if (check_valid(string)) printf("yes\n");
  80. else printf("no\n");
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0s 5272KB
stdin
))((()
stdout
no