본문으로 바로가기

[백준]9012번 문제

category 카테고리 없음 2017. 7. 18. 13:18


스택 괄호문제!!


#include<iostream>

#include<string>

using namespace std;

 

int main() {

 

    char stack[51];

    string str;

    int empty = 0;

    int n;

    int top = -1;

 

    cin >> n;

 

    for (int i = 1; i <= n; i++) 

{

        cin >> str;

        for (int i = 0; i < (int)str.length(); i++) {

            if (str.at(i) == '(')

stack[++top]= str.at(i);

            else 

{

                if (top == -1) 

{

                    empty = 1;

                }

                else stack[top--];

            }

        }


        if (top == -1 && empty == 0)

cout << "YES" << endl;

        else 

cout << "NO" << endl;

        

empty = 0;

        top = -1;


    }

    return 0;

}