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