스택을 사용한 쉬운 문제이다.
'('의 경우 언제나 스택에 push하고,
')'의 경우 스택에 상쇄될 수 있는 '('이 있으면 상쇄, 없으면 push하는 방식으로 해결 할 수 있다.
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
void input_string(string &str)
{
cin >> str;
return;
}
void find_answer(string str)
{
int i;
int answer;
stack<char> st;
for (i = 0; i < str.length(); i++)
{
if (str[i] == ')')
{
if (!st.empty() && st.top() == '(')
{
st.pop();
}
else
{
st.push(')');
}
}
else//str[i] == '('
{
st.push('(');
}
}
cout << st.size() << "\n";
return;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string str;
input_string(str);
find_answer(str);
return 0;
}