개발일지/알고리즘

백준 9012번 : 괄호

김진우 개발일지 2025. 1. 24. 18:54
#include <iostream>
#include <string>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;
	cin.ignore();

	for (int i = 0; i < N; i++)
	{
		string str;
		getline(cin, str);
		int open = 0;
		for(char ch : str)
		{
			if (ch == '(')
			{
				open++;
			}
			else if (ch == ')')
			{
				open--;
				if (open < 0)
				{
					break;
				}
			}
		}
		if (open == 0)
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}