')'가 입력될 때 이전 괄호가 '('이면 현재까지 저장된 '(' 개수를 더해주고 이전 괄호가 ')'라면 1을 더해준다.
#include<iostream>
#include<string>
#include<stack>
using namespace std;
stack<char> s;
int main() {
string str;
cin >> str;
int res = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(') {
s.push(str[i]);
}
else {
s.pop();
if (str[i - 1] == '(') {
res += s.size();
}
else {
res ++;
}
}
}
cout << res;
}
'PS > 백준' 카테고리의 다른 글
[C++] 백준 20046번: Road Reconstruction (0) | 2021.09.09 |
---|---|
[C++] 백준 6764번: Sounds fishy! (0) | 2021.03.13 |
[C++] 백준 16206번: 롤케이크 (0) | 2021.01.23 |
[C++] 백준 2110번: 공유기 설치 (0) | 2020.12.30 |
[C++] 백준 10870번: 피보나치 수 5 (0) | 2020.12.27 |
댓글