fork(1) download
  1. /// 백준 - 4949 균형잡힌 세상
  2.  
  3. // 문자열이 주어졌을 때, 괄호들의 균형이 잘 맞춰져있는지 판단
  4. // 괄호의 종류는 소괄호 (), 대괄호 []
  5.  
  6. // 각 문자열은 알파벳, 공백, 소괄호, 대괄호로 이뤄지며, 온점 "."으로 끝나고, 길이는 100글자 이하
  7. // 각 줄마다 균형을 이루면 yes, 아니면 no를 출력
  8.  
  9. #include <bits/stdc++.h>
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14. ios::sync_with_stdio(0);
  15. cin.tie(0);
  16.  
  17. // 문장들 입력받아서 lines에 넣기
  18. vector<string> lines;
  19. string line;
  20. while(getline(cin, line)){
  21. lines.push_back(line);
  22. if (line == ".") break;
  23. }
  24.  
  25. // lines에서 하나씩 빼서 사용
  26. for (int i=0; i<lines.size(); i++){
  27. if (lines[i] == ".") {break;}
  28. stack<char> stack;
  29. for (int num=0; num<lines[i].length(); num++){
  30. if (lines[i][num] == '(' || lines[i][num] == '[') {stack.push(lines[i][num]);}
  31. else if (lines[i][num] == ')'){
  32. if (stack.empty() || stack.top() != '(') {cout << "no" << '\n'; break;}
  33. else if (stack.top() == '(') stack.pop();
  34. }
  35. else if (lines[i][num] == ']'){
  36. if (stack.empty() || stack.top() != '[') {cout << "no" << '\n'; break;}
  37. else if (stack.top() == '[') stack.pop();
  38. }
  39. else if (lines[i][num] == '.') {
  40. cout << "yes" << '\n'; break;
  41. }
  42. }
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 5300KB
stdin
(.
.
stdout
yes