https://www.acmicpc.net/problem/4949
스택을 활용 기초 문제였다.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <limits.h>
#include <queue>
#include <set>
#include <math.h>
#include <stack>
#include <deque>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while (1) {
string s;
bool flag = true;
stack<char> stk;
getline(cin, s);
// 마무리 조건
if (s.size() == 1 && s[0] == '.') {
break;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(' || s[i] == '[') {
stk.push(s[i]);
}
else if (s[i] == ']') {
if (!stk.empty()) {
if (stk.top() != '[') {
flag = false;
break;
}
stk.pop();
}
else {
flag = false;
break;
}
}
else if (s[i] == ')') {
if (!stk.empty()) {
if (stk.top() != '(') {
flag = false;
break;
}
stk.pop();
}
else {
flag = false;
break;
}
}
}
if (stk.empty() && flag) {
cout << "yes\n";
}
else {
cout << "no\n";
}
}
return 0;
}
'PS > BOJ' 카테고리의 다른 글
[백준] 2504. 괄호의 값(c++) (0) | 2025.02.16 |
---|---|
[백준] 10799. 쇠막대기(c++) (0) | 2025.02.12 |
[백준] 11003. 최솟값 찾기(c++) (0) | 2025.02.05 |
[백준] 5430. AC(c++) (0) | 2025.02.05 |
[백준] 1021. 회전하는 큐(c++) (0) | 2025.02.05 |