문제
#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;
int main(void) {
int n = 0;
string input;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
stack <char> s;
int size = input.size();
for(int k =0;k<size;k++){
if (input[k] == '(') {
s.push('(');
}
else {
if (!s.empty()) {//비어있지 않으면 (가 존재한다는 뜻
s.pop();
}
else {
break;
}
}
}
if (s.empty()) cout << "YES" << endl;
else cout << "NO" <<endl;
}
return 0;
}
왼쪽 괄호만 stack에 넣고,
오른쪽 괄호인 경우에 stack.pop()을 한다.
-> stack.empty() 이면 YES.
'programming language > Structure' 카테고리의 다른 글
[프로그래머스 Level3 그래프] 가장 먼 노드 (0) | 2021.08.01 |
---|---|
[프로그래머스 Level2 힙] 더 맵게 (0) | 2021.07.30 |
트리, 힙, 그래프 (0) | 2021.07.30 |
[프로그래머스 Level2 스택] 주식가격 (0) | 2021.07.29 |
[프로그래머스 Level2 스택] 다리를 지나는 트럭 (0) | 2021.07.29 |