본문 바로가기
백준

[2504] 괄호의 값

by Jcoder 2018. 7. 22.


#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;
else
return sum;
}


'백준' 카테고리의 다른 글

[1966] 프린터 큐  (0) 2018.07.22
[10845] 큐  (0) 2018.07.22
[9012] 괄호  (0) 2018.07.22
[10828] 스택  (0) 2018.07.21
[4948] 베르트랑 공준  (0) 2018.07.21