프로그래밍/문제풀이

[BOJ] 10799 쇠막대기

Jaerae Kim 2020. 6. 13. 02:12
#include <iostream>
#include <stack>
using namespace std;

string str;

int main(void)
{
    cin >> str;
    stack<char> s;
    char before = ' ';
    int ans = 0;

    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '(')
            s.push(str[i]);
        else {
            if (before == '(') {
                s.pop();
                ans += s.size();
            }
            else if (before == ')') {
                s.pop();
                ans++;
            }
        }
        before = str[i];
    }
    printf("%d", ans);

    return 0;
}

풀이 : 6분

 

코딩 : 4분

 

디버깅 : 1분
이전 문자를 보고 분기하는데 이전 문자 업데이트를 하지 않음