BOJ [괄호의 값] 2504
[-] Collapse
#include<cstdio>
#include<cstring>
#include<stack>
int main(){
char str[32]; scanf("%s", str);
int len = strlen(str), ans = 0, mul = 1;
std::stack<char> st;
for (int i = 0; i < len; i++){
if (str[i] == '('){
mul *= 2, st.push(str[i]);
if (str[i + 1] == ')') ans += mul;
}
else if (str[i] == '['){
mul *= 3, st.push(str[i]);
if (str[i + 1] == ']') ans += mul;
}
else if (!st.empty() && str[i] == ')')
mul /= 2, st.pop();
else if (!st.empty() && str[i] == ']')
mul /= 3, st.pop();
}
printf("%d", st.empty() ? ans : 0);
}
#include<cstring>
#include<stack>
int main(){
char str[32]; scanf("%s", str);
int len = strlen(str), ans = 0, mul = 1;
std::stack<char> st;
for (int i = 0; i < len; i++){
if (str[i] == '('){
mul *= 2, st.push(str[i]);
if (str[i + 1] == ')') ans += mul;
}
else if (str[i] == '['){
mul *= 3, st.push(str[i]);
if (str[i + 1] == ']') ans += mul;
}
else if (!st.empty() && str[i] == ')')
mul /= 2, st.pop();
else if (!st.empty() && str[i] == ']')
mul /= 3, st.pop();
}
printf("%d", st.empty() ? ans : 0);
}
댓글 없음:
댓글 쓰기