백준
[2504] 괄호의 값
Jcoder
2018. 7. 22. 15:47
#include <iostream>#include <stack>#include <string>using namespace std;int checkVPS(string str);int main(){string str;cin >> str;cout << checkVPS(str) << endl;return 0;}int checkVPS(string str){stack <char> st;int num = 1, sum = 0;bool value = false;for (int i = 0; i < str.length(); i++){if (str[i] == '('){num *= 2;st.push(str[i]);}else if (str[i] == '['){num *= 3;st.push(str[i]);}else if (str[i] == ')'){if (st.empty()){value = true;break;}if (str[i - 1] == '('){sum += num;st.pop();}num /= 2;}else if (str[i] == ']'){if (st.empty()){value = true;break;}if (str[i - 1] == '['){sum += num;st.pop();}num /= 3;}}if (value || !st.empty())return 0;elsereturn sum;}