์‹ค๋ฒ„4 : ์Šคํƒ ๋ฌธ์ œ์ด๋‹ค.

Code

//
//  main.cpp
//  algorithm_prac
//
//  Created by ์ตœ์™„์‹ on 2021/04/05.
//
 
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int T;
 
int main(){
    cin >> T;
    for (int t = 0; t < T; t++) {
        char input[100];
        vector<char> v;
        cin >> input;
        long long int n =  strlen(input);
        string check = "YES";
        
        for (int i = 0; i < n; i++) {
            if (input[i] == ')') {
                if (!v.empty()) {
                    if (v.back() == '(') {
                        v.pop_back();
                    } else {
                        check = "NO";
                        break;
                    }
                } else {
                    check = "NO";
                }
                
            } else {
                v.push_back(input[i]);
            }
        }
        
        if (!v.empty()) {
            check = "NO";
        }
        
        cout << check << '\n';
        
    }
}
 

Reference