Wednesday, March 9, 2016

UVa 637 - Parentheses Balance

//aarifshuvo  ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

int main()
{
    string s;
    int t;
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        getline(cin,s);

        stack<char> st;
        int fl = 0;

        REP(i,SZ(s))
        {
            if(s[i]=='(' or s[i]=='[') st.push(s[i]);
            else if(s[i]==')' or s[i]==']')
            {
                if(SZ(st))
                {
                    if(st.top()=='(' and s[i]==')') st.pop();
                    else if(st.top()=='[' and s[i]==']') st.pop();
                    else fl=1;
                }
                else fl=1;
            }
            if(fl) break;
        }
        if(SZ(st)) fl=1;
        if(fl) puts("No");
        else puts("Yes");
    }

    return 0;
}

No comments:

Post a Comment