Uva 673 - Parentheses Balance

/*
    Uva 673 - Parentheses Balance
    Verdict:: Accepted
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint scanf ("%d %d",&a,&b)
#define sfl scanf ("%ld %ld",&a,&b)
#define sfll scanf ("%lld %lld",&a,&b)
#define sfd scanf ("%lf %lf",&a,&b)
#define sff scanf ("%f %f",&a,&b)
#define loop(i,n) for(i=0;i<n;i++)
#define LL long long
#define L long
#define nl puts("")
#define MX 1000005
#define N 100
#define MOD 100000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
#define ps push
#define clr clear
#define bn begin()
#define ed end()

using namespace std;

int main()
{
    int t;
    sf("%d\n",&t);
    while(t--)
    {
        string s;
        int i;
        bool f = false;

        stack<char>st;

        getline(cin,s);

        int len=s.size();


        if(!s.compare("\n")) // ----- (a)
        {
            pf("Yes\n");
            continue;
        }

        /*if(s=="\n") // ---- (b) ; and (a) and (b) are equal.. now it's your choice.
        {
            pf("Yes\n");
            continue;
        }*/

        loop(i,len)
        {
            if(s[i]=='(' or s[i]=='[')
            {
                st.push(s[i]);
            }

            else if (s[i]==')' and !st.empty() and st.top()=='(')
            {
                st.pop(); //f = true;
            }

            else if (s[i]==']' and !st.empty() and st.top()=='[')
            {
                st.pop(); //f = true;
            }

           else f = true;
        }

        int len2=st.size(); //cout << len2;

        if((!f) and len2==0)
        {
            pf("Yes\n");
        }

        else
        {
            pf("No\n");
        }

        f=false;
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number