UVA 673 Parentheses Balance Solution

Solve in C++:


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int l,i,n,j;
    while(scanf("%d",&n)!=EOF)
    
        getchar();
        for(i=1; i<=n; i++)
        {
            string str;
            stack<char>s;
            getline(cin,str);
            int flag=0;
            for(j=0; j<str.size(); j++)
            {
                if(str[j]=='('||str[j]=='[')
                {
                    s.push(str[j]);
                }
                else if(((!s.empty())&&(s.top()=='('))&&(str[j]==')'))
                {
                    s.pop();
                }
                else if(((!s.empty())&&(s.top()=='['))&&(str[j]==']'))
                {
                    s.pop();
                }
                else
                {
                    flag=1;
                }
            }
            if(flag==1||s.size()>=1)
                printf("No\n");
            else
                printf("Yes\n");

        }
    }
}

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.