#include <stdio.h>

//stack top 전역 변수 안하고 했을때
//푸쉬 함수
void push(char *stack, int *top, char val) {
    stack[++(*top)] = val;
}
//팝 함수
char pop(char *stack, int *top){
    //스택이 비었을때
    if((*top)==-1) return 0;
    return stack[(*top)--];
}
//괄호가 이쁜지
int VPS (const char *arr) {
    char stack[100];
    int top=-1;
    for(int i=0; arr[i]; i++) {
        char x=arr[i];
        if(x=='(') {
            push(stack,&top,x);
        } else if (x==')') {
            char open = pop(stack,&top);
            if(open==0) return 0;
        }
    }
    return (top == -1);
}


int main() {
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++) {
        char ps[51]={0};
        scanf("%s",ps);
    
        if(VPS(ps)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: