- stk.top()이 아닌 str[i-1](=이전 입력 값)을 활용하는 문제
- stk을 다룰 때, stk.top()만 사용한다는 고정관념에서 벗어나서 유연한 사고를 하자.
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ans = 0; string str; cin >> str; stack<char> stk; for(int i = 0; i < str.size(); ++i){ if(str[i] == '(') stk.push(str[i]); else if(str[i] == ')'){ stk.pop(); //막대의 끝일 경우 if(str[i-1] == ')') ++ans; //레이저일 경우 else ans += stk.size(); } } cout << ans; return 0; }