[백준] 10799번

Jeanine·2022년 3월 11일
0

baekjoon

목록 보기
14/120
post-thumbnail
post-custom-banner

💻 C++ 기반

https://www.acmicpc.net/problem/10799

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string str;
    cin >> str;

    stack<char> s;
    int numOfSticks = 0;
    int numOfPieces = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] == '(')
        {
            numOfSticks++;
        }
        else if (str[i] == ')')
        {
            if (s.top() == '(')
            {
                numOfSticks--;
                numOfPieces += numOfSticks;
            }
            else
            {
                numOfSticks--;
                numOfPieces++;
            }
        }
        s.push(str[i]);
    }

    cout << numOfPieces;

    return 0;
}
profile
Grow up everyday
post-custom-banner

0개의 댓글