2504번 괄호의 값

2504번 괄호의 값 백준 알고리즘 문제

음, 일단 최근에 푼 것 중에 시간이 가장 오래걸렸던 것 같다. 다른 분들의 풀이를 보니, 조금 돌아서 푼듯하다. 방법적인 측면이니 무시를 하더라도, 코드를 최대한 깔끔하게 짜려하지 않으면 예상치 못한 에러들이 많이 생겨나는 것을 발견하였다.


이 문제는 스택을 활용하는 문제이다. 꽤나 좋은 문제였던 것 같다. 다음 번에 기회가 되면 최대한 깔끔하게 풀어보자. 참고로 반례를 해결하는 게 상당히 까다로웠는데, 반례를 차단하는 방법 또한 잘 익히도록 노력하자.

//괄호의 값
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string str;
int arr[50] = {0};
stack<int> st;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);  // cin 실행속도 향상
  cin >> str;
  for (int i = 0; i < str.length(); i++) {
    if (str[i] == '(')
      arr[i] = -1;
    else if (str[i] == ')')
      arr[i] = -2;
    else if (str[i] == '[')
      arr[i] = -3;
    else if (str[i] == ']')
      arr[i] = -4;
    else {
      cout << "0" << endl;
      return 0;
    }
  }

  for (int i = 0; i < str.length(); i++) {
    if (!st.empty()) {
      int top = st.top();
      // '(' 일 때 그냥 푸쉬한다.

      if (arr[i] == -1) {
        st.push(arr[i]);
      }
      // '[' 일 때 그냥 푸쉬한다.
      else if (arr[i] == -3) {
        st.push(arr[i]);
      }
      // '(' 즉 -1 를 찾을 때까지 앞으로 와야 되고, 중간에 숫자가 있는 것들은
      // 모두 더해준다.
      else if (arr[i] == -2) {
        // 스택이 비었을 때까지 비었을 때까지 못찾으면 error
        int temp = 0;
        bool check = false;
        if (st.empty()) {
          cout << "0" << endl;
          return 0;
        }
        while (!st.empty()) {
          int ttop = st.top();

          if (ttop > 0) {
            temp += ttop;

            st.pop();
          } else if (ttop == -1) {
            if (temp == 0) {
              temp += 2;
            } else
              temp = temp * 2;
            st.pop();
            check = true;
            break;
          } else {
            cout << 0 << endl;
            return 0;
          }
        }
        if (!check) {
          cout << 0 << endl;
          return 0;
        }

        st.push(temp);
      } else if (arr[i] == -4) {
        // 스택이 비었을 때까지 비었을 때까지 못찾으면 error
        int temp = 0;
        bool check = false;
        if (st.empty()) {
          cout << "0" << endl;
          return 0;
        }
        while (!st.empty()) {
          int ttop = st.top();
          if (ttop > 0) {
            temp += ttop;
            st.pop();
          } else if (ttop == -3) {
            // 3를 곱해준다.
            if (temp == 0) {
              temp += 3;
            } else {
              temp = temp * 3;
            }
            st.pop();
            check = true;
            break;
          } else {
            cout << 0 << endl;
            return 0;
          }
        }
        if (!check) {
          cout << 0 << endl;
          return 0;
        }
        st.push(temp);
      }
    } else {
      if (arr[i] == -2 || arr[i] == -4) {
        cout << 0 << endl;
        return 0;
      }
      st.push(arr[i]);
    }
  }
  int res = 0;

  while (!st.empty()) {
    int top = st.top();
    if (top < 0) {
      cout << "0" << endl;
      return 0;
    } else {
      res += top;
      st.pop();
    }
  }
  cout << res << endl;
  return 0;
}