풀이: 레이저 갯수를 해당 스택의 사이즈만큼에 ++해준다
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
string str;
cin >> str;
stack<char> st;
int val = 0;
int pArr[100000] = {0,};
str+="_";
for(int i = 0; i < str.length(); i++) {
if(str[i] == '(' && str[i+1] == ')') {
pArr[st.size()]++;
i++;
}
else if(str[i] == '(') {
st.push(str[i]);
}
else if(str[i] == ')') {
val += pArr[st.size()]+1;
pArr[st.size()-1] += pArr[st.size()];
pArr[st.size()] = 0;
st.pop();
}
}
cout << val;
}
'개발 > 알고리즘' 카테고리의 다른 글
11052 실버 1) 카드 구매하기 DP (0) | 2024.10.23 |
---|---|
17087 실버 2 ) 숨바꼭질 6 - 유클리드 호제법 (0) | 2024.10.08 |
백준 17413 실버 3 ) 단어 뒤집기 2 (0) | 2024.09.19 |
백준 1874 실버2) 스택 수열 (0) | 2024.09.14 |
백준 실버 5 - 1268) 임시 반장 정하기 - 구현 (0) | 2024.09.04 |