괄호

BiBi·2021년 1월 18일
0

코딩테스트연습

목록 보기
27/66
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
using namespace std;



int main() {
	freopen("input.txt", "rt", stdin);
	int n;
	scanf("%d", &n);

	string str;
	for (int i = 0; i < n; i++) {
		cin >> str;
		if (str[0] == ')') {
			printf("NO\n");
			continue;
		}
		int cnt = 0;
		int flag = 1;
		for (int j = 0; j < str.length(); j++) {
			if (str[j] == '(') {
				cnt++;
			}
			else if (str[j] == ')') {
				cnt--;
			}
			if (cnt < 0) {
				flag = 0;
			}
		}
		if (flag == 1 && cnt==0) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	

	return 0;
}
profile
Server Network Engineer

0개의 댓글