#include <iostream>
#include <stack>
#include <string>
using namespace std;
string balance(string s) {
stack<char> st;
char cur = ' ';
for(int i = 0 ; i <s.length(); i++)
{
if (st.empty() && (s[i]== ')' || s[i] == ']')) {
return "no";
}
if (s[i] == '(' || s[i] == '[') {
st.push(s[i]);
cur = s[i];
}
if ((cur == '(' && s[i] == ']') || (cur == '[' && s[i] == ')')) {
return "no";
}
if ((cur == '(' && s[i] == ')') || (cur == '[' && s[i]==']')) {
st.pop();
if(!st.empty())
cur = st.top();
}
}
if (st.empty())
return "yes";
else
return "no";
}
int main() {
string s;
while (true) {
cin.clear();
getline(cin, s);
if (s == ".")
break;
cout << balance(s) << endl;
}
return 0;
}
'Programming > Algorithm' 카테고리의 다른 글
백준 #10845 - 큐 python (0) | 2019.10.19 |
---|---|
백준 #17298 - 오큰수 python (0) | 2019.10.19 |
백준 #10773 - 제로 python (0) | 2019.10.19 |
백준 #10828 - 스택 python (0) | 2019.10.19 |
백준 #4344 - 평균은 넘겠지 python (0) | 2019.02.12 |