fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class Stack {
  7. int list[100] = { 0 };
  8. int top = -1;
  9. int tmp = 1;
  10. public:
  11. bool ex = true;
  12. void error() {
  13. ex = false;
  14. }
  15. void print() {
  16. cout << list[0];
  17. }
  18. void reset() {
  19. top = -1;
  20. ex = true;
  21. }
  22. int get() {
  23. return top;
  24. }
  25. void Push(int n) {
  26. if (n > 0 && list[top] > 0)
  27. list[top] += n;
  28. else {
  29. top++; list[top] = n;
  30. }
  31. }
  32. void Pop(char c = '\0') {
  33. if (top <= -1) { ex = false; return; }
  34. if (c == ')') {
  35. for (int index = 0; index < 2; index++) {
  36. if (list[top] == -3) {//[일경우
  37. error(); top + index; tmp = 1; return;//
  38. }
  39. else if (list[top]!=-2&&index==0) { //숫자일 경우
  40. tmp *= list[top]; list[top] = 0; top--;
  41. }
  42. else if (list[top] == -2) {
  43. tmp *= 2; list[top] = 0; top--; Push(tmp); tmp = 1; return;
  44. }
  45. else if (list[top] != -2 && index == 1) {
  46. error(); top + index; tmp = 1; return;
  47. }
  48. }
  49. }
  50. else if (c == ']') {
  51. for (int index = 0; index < 2; index++) {
  52. if (list[top] == -2) {//(일경우
  53. error(); top + index; tmp = 1; return;//
  54. }
  55. else if (list[top] != -3 && index == 0) { //숫자일 경우
  56. tmp *= list[top]; list[top] = 0; top--;
  57. }
  58. else if (list[top] == -3) {//[일경우
  59. tmp *= 3; list[top] = 0; top--; Push(tmp); tmp = 1; return;
  60. }
  61. else if (list[top] != -3 && index == 1) {
  62. error(); top + index; tmp = 1; return;
  63. }
  64. }
  65. }
  66. }
  67. };
  68.  
  69. int main() {
  70. int num = 1;
  71. Stack s;
  72. string str;
  73. // cin >> num;
  74. // cin.ignore();
  75. //for (int i = 0; i < num; i++) {
  76. getline(cin, str);
  77. s.reset();
  78. for (int index = 0; str[index] != '\0'; index++) {
  79. if (str[index] == '(') {
  80. s.Push(-2);
  81. }
  82. else if (str[index] == '[')
  83. s.Push(-3);
  84. else if (str[index] == ')') {
  85. s.Pop(')');
  86. }
  87. else
  88. s.Pop(']');
  89. }
  90. if (s.get() == 0 && s.ex) s.print();
  91. else cout<<0;
  92. //}
  93. }
Success #stdin #stdout 0s 4372KB
stdin
(()[[]])([])
stdout
Standard output is empty