본문 바로가기
개발 노트/백준, 프로그래머스 풀이

[백준 9012/c++] 괄호 - 문자열 - 스택

by tokkiC 2022. 6. 23.

스택 문제는 전에 풀어본 적이 있어서 아주 쉽게 풀었다

다른 문제도 이렇게만 풀렸으면...

#include <bits/stdc++.h>
using namespace std;



int main(){
	int n;
	cin >> n;
	
	for(int i=0; i<n; i++){
		string s;
		stack<char> stk;
		
		cin >> s;
		
		for(auto k : s){
			if((stk.size()!=0) && (stk.top()=='(') && k==')'){
				stk.pop();
			} else{
				stk.push(k);
			}			
		}
		if(stk.size()!=0){
			cout << "NO" << "\n";
		} else {
			cout << "YES" << "\n";
		}
		
	}	
	
	return 0;
}

댓글