C++)
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string str;
while (true) {
getline(cin, str);
if (str == ".")
break;
stack<char> st;
for (auto ele : str) {
if (isalpha(ele) || ele == ' ' || ele == '.')
continue;
if (ele == '(' || ele == '[')
st.push(ele);
else if (!st.empty() && st.top() == '(' && ele == ')')
st.pop();
else if (!st.empty() && st.top() == '[' && ele == ']')
st.pop();
else
st.push(ele);
}
if (st.empty())
cout << "yes\n";
else
cout << "no\n";
}
return 0;
}
Python)
import sys
read = sys.stdin.readline
while True:
temp = read().rstrip()
if temp == '.':
break
st = []
for x in temp:
if x.isalpha() or x == ' ' or x == '.':
continue
if x == '(' or x == '[':
st.append(x)
elif st and st[-1] == '(' and x == ')':
st.pop()
elif st and st[-1] == '[' and x == ']':
st.pop()
else:
st.append(x)
print("no" if st else "yes")'백준 1 > 자료구조' 카테고리의 다른 글
| [백준 1918] 후위 표기식 (C++/Java) (0) | 2020.12.06 |
|---|---|
| [백준 1874] 스택 수열 (C++/Python) (0) | 2020.12.06 |
| [백준 10866] 덱 (C++/Python) (0) | 2020.12.06 |
| [백준 1158] 요세푸스 문제 (C++/Python) (0) | 2020.12.06 |
| [백준 2161] 카드1 (C++/Python) (0) | 2020.12.06 |