B304: Parentheses Balance https://zerojudge.tw/ShowProblem?problemid=b304



#include<bits/stdc++.h>
#include <stack>
using namespace std;

int main(void){

    int n;
    string str;
    stack<int> stk;

    cin>>n;

    for(int i = 0 ;i<n;i++){

        cin>>str;
        bool pass = true;

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

            if(str[j]=='('||str[j]=='['){
                stk.push( str[j] );
            }
            else if(stk.empty()){
                pass=false;
                break;
            }
            else if(str[j]==')'){
                if(stk.top()!='('){
                    pass=false;
                    break;
                }
                stk.pop();
            }
            else if(str[j]==']'){
                if(stk.top()!='['){
                    pass=false;
                    break;
                }
                stk.pop();
            }
        }

        if(!stk.empty()){
            pass = false;
        }

        if(pass==true)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;

    }

}

results matching ""

    No results matching ""