https://www.acmicpc.net/problem/9012


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stack>
#include <stdio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    int N;
    unsigned long long count = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        stack<char> stack1;
        string input;
        cin >> input;
        while (!stack1.empty())
            stack1.pop();
        for (int i = 0; i < input.size(); i++) {
            if (stack1.empty())
                stack1.push(input[i]);
            else {
                if (input[i] == '(')
                    stack1.push(input[i]);
                else if (input[i] == ')')
                    if (stack1.top() == '(')
                        stack1.pop();
                    else
                        stack1.push(input[i]);
            }
        }
        if (stack1.empty())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}
cs


+ Recent posts