Algorithm
[BOJ] 4949번 : 균형잡힌 세상
rudgh99_algo
2024. 8. 8. 10:28
1. problem :
https://www.acmicpc.net/problem/4949
2. solution 1 :
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
while (true) {
string words;
stack<char> s;
getline(cin, words);
bool isBalanced = true;
if (words == ".") break;
for (auto c : words) {
if (c == '[' || c == '(') s.push(c);
else if (c == ']' || c == ')') {
if (s.empty()) { // 예외처리 : stack이 비었는데, 마지막 조건에서 stack이 비지않았음을 통과해, yes로 출력
isBalanced = false;
break;
}
if ((c == ']' && s.top() == '[') || (c == ')' && s.top() == '(')) s.pop();
else {
isBalanced = false;
break;
}
}
}
if (!s.empty()) isBalanced = false; // stack에 [ or (이 남아있는경우 고려
if ((isBalanced)) cout << "yes" << '\n';
else cout << "no" << '\n';
}
return 0;
}