728x90
문제 9012번
https://www.acmicpc.net/problem/9012
스택에 입력값을 넣는데 맨 위가 (고, 그 다음에 올 괄호가 )라면 pop을 통해 맨 위의 (를 빼준다. 그런 식으로 스택에 아무것도 남지 않는다면 YES를 출력하고, 남아있다면 짝이 맞지 않다는 뜻이기 때문에 NO를 출력한다.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
stack<char> st;
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (st.empty()) {
st.push(s[i]);
}
else {
if (st.top() == '(' && s[i] == ')') {
st.pop();
}
else {
st.push(s[i]);
}
}
}
if (st.empty()) {
cout << "YES" << '\n';
}
else {
cout << "NO" << '\n';
}
}
return 0;
}

728x90
'백준 > C++' 카테고리의 다른 글
[C++] 백준 14425번 - 문자열 집합 (1) | 2024.06.12 |
---|---|
[C++] 백준 11721번 - 열 개씩 끊어 출력하기 (0) | 2024.06.12 |
[C++] 백준 10039번 - 평균 점수 (0) | 2024.06.09 |
[C++] 백준 2522번 - 별 찍기 12 (0) | 2024.06.07 |
[C++] 백준 2446번 - 별 찍기 9 (0) | 2024.06.06 |