4949 균형잡힌 세상
2019. 7. 31. 13:28ㆍ알고리즘/백준
\n이 포함되지 않는 한 줄 처리는 getline으로 한다
그리고 반드시 끝난 후 스택이 비워졌는지도 검사를 해야한다
문제: https://www.acmicpc.net/problem/4949
깃허브주소: https://github.com/surinoel/boj/blob/master/4949.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stack> | |
#include <sstream> | |
#include <iostream> | |
using namespace std; | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
string s; | |
for (;;) { | |
getline(cin, s); | |
if (s == ".") break; | |
stack<char> st; | |
bool ok = true; | |
for (int i = 0; i < s.length() - 1; i++) { | |
if (s[i] == '(' || s[i] == '[') { | |
st.push(s[i]); | |
} | |
else if (s[i] == ')') { | |
if (st.empty() || st.top() != '(') { | |
ok = false; | |
} | |
else { | |
st.pop(); | |
} | |
} | |
else if (s[i] == ']') { | |
if (st.empty() || st.top() != '[') { | |
ok = false; | |
} | |
else { | |
st.pop(); | |
} | |
} | |
} | |
if (ok && st.empty()) { | |
cout << "yes\n"; | |
} | |
else { | |
cout << "no\n"; | |
} | |
} | |
return 0; | |
} |
'알고리즘 > 백준' 카테고리의 다른 글
1543 문서 검색 (0) | 2019.08.01 |
---|---|
17363 우유가 넘어지면? (0) | 2019.07.31 |
2503 숫자 야구 (0) | 2019.07.30 |
6987 월드컵 (1) | 2019.07.30 |
5586 JOI와 IOI (0) | 2019.07.28 |