출처 - https://www.acmicpc.net/problem/9012

 

[ 코드 ]

# include <iostream>
# include <string>
# include <stack>
usingnamespace std;

int main()
{
    int oper;
    cin >> oper;
    string str;
    while (oper) {
        int sp = 0;
        stack < char > s;
        cin >> str;
        for (int i = 0; i < str.length(); ++i) {
            if (str[i] == '(') {
                s.push(str[i]);
            }
            else {
                if (!s.empty()) {
                    s.pop();
                }
                else {
                    sp = 1;
                    break;
                }
            }
        }
        if (!s.empty()) {
            sp = 1;
        }
        if (sp == 0) cout << "YES" << endl;
        else cout << "NO" << endl;
        --oper;
    }
    return 0;
}