9012 - 괄호

solution

code

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

int main()
{
    string input;
    int T;
    cin >> T;
    int depth;
    bool vps;
    for(int i = 0; i < T; i++)
    {
        depth = 0;
        vps = true;
        cin >> input;
        for(int j = 0; j < input.size(); j++)
        {
            if(input[j] == '(') depth++;
            else depth--;
            if(depth < 0)
            {
                vps = false;
                break;
            }
        }
        if(!vps || depth > 0) cout << "NO" << endl;
        else cout << "YES" << endl;
    }

    return 0;
}

ref

9012번: 괄호