백준 문제풀이 9012번 괄호 문제(C++)

2024. 4. 30. 15:35백준문제풀이

https://www.acmicpc.net/problem/9012

#include<iostream>
#include<stack>
using namespace std;

int main() {
	int num;
	cin >> num;
	while (num--)
	{
		stack<int> s1,s2;

		string s;
		cin >> s;
		
		for (char c : s) {
			if (c == '(') s1.push(0);
			else if (s1.empty() && c == ')') {
				s1.push(0);
				break;
			}
			else {
				s1.pop();
			}
		}

		if (s1.empty()) {
			cout << "YES"<<endl;
		}
		else {
			cout << "NO"<<endl;
		}
	}


}