cgy12306
[백준 BoJ] 2504 - 괄호의 값 본문
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i = 0, sum = 0, tmp = 1, flag = 0;
stack<char> s;
string str;
cin >> str;
while (str[i] != 0) {
if (str[i] == '(') {
s.push(str[i]);
tmp *= 2;
}
else if (str[i] == '[') {
s.push(str[i]);
tmp *= 3;
}
else if (str[i] == ')' && (s.empty() || s.top() != '(')) {
flag = 1;
break;
}
else if (str[i] == ']' && (s.empty() || s.top() != '[')) {
flag = 1;
break;
}
else if(str[i] == ')'){
if (str[i - 1] == '(') {
sum += tmp;
}
s.pop();
tmp /= 2;
}
else if (str[i] == ']') {
if (str[i - 1] == '[') {
sum += tmp;
}
s.pop();
tmp /= 3;
}
else {
flag = 1;
break;
}
i++;
}
if (flag == 1 || !s.empty()) {
cout << '0';
}
else {
cout << sum;
}
return 0;
}
'Algorithm > C++' 카테고리의 다른 글
C++ String 출력 방식 (0) | 2021.03.28 |
---|---|
[백준 BoJ] 1699 - 제곱수의 합 (0) | 2021.03.26 |
[백준 BoJ] 10845 - 큐 (0) | 2021.03.25 |
[백준 BoJ] 10828 - 스택 (0) | 2021.03.25 |
[백준 BoJ] 2748 - 피보나치 수 2 (0) | 2021.03.25 |
Comments