https://www.acmicpc.net/problem/4949
4949번: 균형잡힌 세상
하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 입력의 종료조건으로 맨 마
www.acmicpc.net
#include<iostream>
#include<string>
#include<stack>
#include<vector>
using namespace std;
int main() {
char ch;
int check = 0;
string str;
stack<char> s;
while (true) {
getline(cin, str);
if (str[0] == '.') break;
for (int i = 0; i < str.length(); i++) {
ch = str[i];
if (ch == '(') {
s.push('(');
}
else if (ch == '[') {
s.push('[');
}
else if (ch == ')') {
if (!s.empty() && s.top() == '(') {
s.pop();
}
else {
cout << "no\n";
check++;
break;
}
}
else if (ch == ']') {
if (!s.empty() && s.top() == '[') {
s.pop();
}
else {
cout << "no\n";
check++;
break;
}
}
}
if (s.empty() && check == 0) {
cout << "yes\n";
}
else if (!s.empty() && check == 0) {
cout << "no\n";
}
while (!s.empty()) {
s.pop();
}
check = 0;
}
}
'Study > 백준' 카테고리의 다른 글
[백준] 11724번: 트리의 부모 찾기(C++) (0) | 2022.04.10 |
---|---|
[백준] 1676번: 팩토리얼 0의 개수 (0) | 2022.03.30 |
[백준] 1158번: 요세푸스 문제 (0) | 2022.03.29 |
[백준] 2004번: 조합 0의 개수 (0) | 2022.03.29 |
[백준] 10799번: 쇠막대기 (C++) (0) | 2022.03.29 |