Skip to content

Instantly share code, notes, and snippets.

@honux77
Last active April 18, 2019 15:17
Show Gist options
  • Select an option

  • Save honux77/efa9f44b58890c789ff4ac993e2e63f7 to your computer and use it in GitHub Desktop.

Select an option

Save honux77/efa9f44b58890c789ff4ac993e2e63f7 to your computer and use it in GitHub Desktop.
BOJ 2504 - wring
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stack>
#include <iostream>
#include <string>
using namespace std;
const int SL = -1;
const int LL = -2;
bool check(string &r) {
stack<char> st;
for (auto c : r) {
switch (c) {
case '[':
case '(':
st.push(c);
break;
case ')':
if (st.empty() || st.top() != '(') return false;
st.pop();
break;
case ']':
if (st.empty() || st.top() != '[') return false;
st.pop();
break;
}
}
return st.empty();
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string str;
cin >> str;
stack<int> ss;
if (!check(str)) {
cout << 0 << endl;
return 0;
}
for (auto c : str) {
int v = 0;
switch (c) {
case '(':
ss.push(SL);
break;
case '[':
ss.push(LL);
break;
case ')':
while (ss.top() != SL) {
v += ss.top();
ss.pop();
}
ss.pop();
if (v == 0) {
ss.push(2);
}
else {
ss.push(v * 2);
}
break;
case ']':
while (ss.top() != LL) {
v += ss.top();
ss.pop();
}
ss.pop();
if (v == 0) {
ss.push(3);
}
else {
ss.push(v * 3);
}
break;
}
}
int ret = 0;
while (!ss.empty()) {
ret += ss.top();
ss.pop();
}
cout << ret << endl;
return 0;
}
@honux77
Copy link
Author

honux77 commented Apr 18, 2019

check() 함수 오류를 수정해서 통과했습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment