fork download
  1. #include <iostream>
  2. #include<string>
  3. #include <stack>
  4. using namespace std;
  5.  
  6.  
  7. //조건 3개
  8. //스택에서 pop 하면서 ')'갯수보다 '('가 많아지면 탈락
  9. //']'뽑은 이후에 '(' 나오거나, ')' 이후에 '['나오면 게임 끝
  10. //'('와 ')'의 갯수가 동일해야함
  11.  
  12.  
  13. bool isVps(stack<char>& ref)
  14. {
  15. int sRightN = 0, sLeftN = 0; //소괄호 갯수
  16. int bRightN = 0, bLeftN = 0; //대괄호 갯수
  17. char beforC = ' '; //이전 문자
  18. char nowC = ' '; //현재 문자
  19.  
  20.  
  21. int stackSize = ref.size();
  22.  
  23. for (int i = 0; i < stackSize; i++) //스택에서 pop 하면서 ')'갯수보다 '('가 많아지면 탈락
  24. {
  25.  
  26. beforC = ref.top();
  27.  
  28.  
  29. if (ref.top() == ')')
  30. sRightN++;
  31. else if (ref.top() == '(')
  32. sLeftN++;
  33. else if (ref.top() == ']')
  34. bRightN++;
  35. else if (ref.top() == '[')
  36. bLeftN++;
  37.  
  38. if (sLeftN > sRightN)
  39. return false;
  40. else if (bLeftN > bRightN)
  41. return false;
  42. else
  43. {
  44. ref.pop();
  45. if(!ref.empty())
  46. nowC = ref.top();
  47. }
  48.  
  49. if ((nowC == '(' && beforC == ']') || (nowC == '[' && beforC == ')')) //']'뽑은 이후에 '(' 나오거나, ')' 이후에 '['나오면 게임 끝
  50. return false;
  51.  
  52. }
  53.  
  54. if (sLeftN == sRightN && bLeftN == bRightN) //'('와 ')'의 갯수가 동일해야함
  55. return true;
  56. else
  57. return false;
  58. }
  59.  
  60. //반례
  61. //()
  62. //.
  63.  
  64. int main() {
  65. ios::sync_with_stdio(false);
  66. cin.tie(NULL);
  67.  
  68. string line;
  69. stack<char> vpsContainer;
  70.  
  71.  
  72. while (true)
  73. {
  74.  
  75. while (true) //문장의 마지막 '.'을 받을 때까지 계속 스택에 넣음
  76. {
  77. getline(cin, line);
  78. for (char c : line)
  79. if (c == '.')
  80. goto out;
  81. else if (c == '(' || c == ')' || c == '[' || c == ']')
  82. vpsContainer.push(c);
  83.  
  84. }
  85.  
  86. out:
  87.  
  88. if (line == ".")
  89. break;
  90.  
  91.  
  92. if (isVps(vpsContainer))
  93. cout << "yes" << "\n";
  94. else
  95. cout << "no" << "\n";
  96.  
  97. while (!vpsContainer.empty())//스택 컨터이너 비우기
  98. {
  99. vpsContainer.pop();
  100. }
  101.  
  102. }
  103.  
  104.  
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0.01s 5280KB
stdin
[(()]).
.
stdout
yes