[Algorithm] - 백준 2504 괄호의 값

억지로 꾸역꾸역 풀었는데...

올림피아드 초등부 중등부 문제라니...

게다가 하드코딩 수준인데 아직도 공부할게 많다...


#include <iostream>
#include <stack>
using namespace std;

stack<int> st;
int main(){
char str[31];
cin >> str;

if (str[0] == '(') st.push(0);
else if (str[0] == '[') st.push(1);
else {
cout << '0' << endl;
return 0;
}

for (int i = 1; i < 30; i++){
if (str[i] == NULL) break;
else{
int temp = 0;

if (str[i] == '('){
st.push(0);
}
else if (str[i] == '['){
st.push(1);
}
else if (str[i] == ')'){
if (st.top() == 0){
st.pop();
st.push(2);
}
else{
while (st.top() != 0){
if (st.top() == 1){
cout << '0' << endl;
return 0;
}
else{
temp += st.top();
st.pop();
}
}

st.pop();
st.push(temp * 2);
}
}
else{
if (st.top() == 1){
st.pop();
st.push(3);
}
else{
while (st.top() != 1){
if (st.top() == 0){
cout << '0' << endl;
return 0;
}
else{
temp += st.top();
st.pop();
}
}

st.pop();
st.push(temp * 3);
}
}
}
}

int result = 0;
while (!st.empty()){
result += st.top();
st.pop();
}
cout << result;

return 0;
}

댓글