안선생의 개발 블로그

백준 11899 괄호 끼워넣기 본문

백준

백준 11899 괄호 끼워넣기

안선생 2023. 2. 9. 15:22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include<stack>
#include<vector>
using namespace std;
 
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
 
 
    stack<char> st;
    string s; cin >> s;
 
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == ')')
        {
            if (!st.empty() && st.top() == '(')
                st.pop();
            else
                st.push(s[i]);
        }
        else
            st.push(s[i]);
    }
    int count = 0;
    while (!st.empty())
    {
        count++;
        st.pop();
    }
    cout << count;
    return 0;
 
}
cs

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

 

11899번: 괄호 끼워넣기

첫 번째 줄에 S를 올바른 괄호열으로 만들기 위해 앞과 뒤에 붙여야 할 괄호의 최소 개수를 출력합니다. 불가능한 경우는 주어지지 않습니다.

www.acmicpc.net

 

'백준' 카테고리의 다른 글

C++ 13305 잃어버린 괄호  (0) 2023.02.10
C++ 11399 ATM  (0) 2023.02.10
C++ 11047 동전  (0) 2023.02.10
백준 17608번 막대기  (0) 2023.02.09
백준 1316  (0) 2023.01.20