스택엔 마커만 넣음: (
→-1
, [
→-2
진행 중 곱(temp
) 유지:
(
만나면 temp *= 2
, [
만나면 temp *= 3
()
·[]
를 만나면 현재 temp
를 ans
에 더함)
는 -1
, ]
는 -2
와만 매칭ans = 0
후 종료0
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int ans = 0;
stack<int> st;
string str;
int temp = 1;
cin >> str;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(') {
st.push(-1);
temp *= 2;
}
else if (str[i] == '[') {
st.push(-2);
temp *= 3;
}
else if (str[i] == ')') {
if (st.empty() || st.top() != -1) {
ans = 0;
break;
}
if(str[i - 1] == '(') {
ans += temp;
temp /= 2;
st.pop();
}
else {
temp /= 2;
st.pop();
}
}
else if (str[i] == ']') {
if (st.empty() || st.top() != -2) {
ans = 0;
break;
}
if(str[i - 1] == '[') {
ans += temp;
temp /= 3;
st.pop();
}
else {
temp /= 3;
st.pop();
}
}
}
if (!st.empty()) ans = 0;
cout << ans;
return 0;
}
)
나 ]
면 즉시 0 처리([])
처럼 곱 누적이 잘 더해지는지