#include <iostream> #include <stack> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); stack<int> s; string str; int cur_val = 1, tot_val = 0; cin >> str; for(int i=0;i<str.length();i++) { char a = str[i]; if(a == '(' || a == '[') { int mul = a == '(' ? 2 : 3; cur_val *= mul; s.push(a); }else if(a == ')' || a == ']') { if(s.empty()) { cout << 0; return 0; } if(str[i-1] != ')' && str[i-1] != ']') { tot_val += cur_val; } int mul = a == ')' ? 2 : 3; cur_val /= mul; if((a == ')' && s.top() != '(') || (a == ']' && s.top() != '[')) { cout << 0; return 0; } s.pop(); } } if(s.empty()) cout << tot_val; else cout << 0; }
- 입력이 올바르지 못한 괄호열은 stack을 사용해서 판별했음
 
1) ')' 일 때 s.top()이 '(' 아니면 false;
2) ']' 일 때 s.top()이 '[' 아니면 false;
3) 닫는 괄호 ')' 또는 ']'가 들어왔을 때 stack.empty() 이면 false;