9012_괄호(C++)

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

int main() {
    int T;
    string command;
    cin >> T;
    for (int i = 0; i < T; i++) {
        stack<char> mine;
        cin >> command;
        int counter = 0;
        for (int j = 0; j < command.length(); j++) {
            if (command[j] == '(') {
                mine.push(command[j]);
            }
            else {
                if (!mine.empty()) mine.pop();
                else break;
            }
            counter = j;
        }
        if (mine.empty() && counter == command.length()-1) cout << "YES" << '\n';
        else cout << "NO" << '\n';
    }
}

스택 설명 참고:
https://velog.io/@kimeunseo58/%EC%8A%A4%ED%83%9D
스택 소스코드:
https://velog.io/@kimeunseo58/10828%EC%8A%A4%ED%83%9DC

좋은 웹페이지 즐겨찾기