[BOJ] 4949번 : 균형잡힌 세상
2024. 8. 8. 10:28ㆍAlgorithm
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;
}'Algorithm' 카테고리의 다른 글
| [BOJ] 10804번 : 카드 역배치 (0) | 2024.08.12 |
|---|---|
| [BOJ] 1267번 : 핸드폰 요금 (0) | 2024.08.12 |
| [BOJ] 6198번 : 옥상 정원 꾸미기 (0) | 2024.08.07 |
| [BOJ] 5430번 : AC (0) | 2024.08.07 |
| [BOJ] 1021번 : 회전하는 큐 (0) | 2024.08.07 |