View Code of Problem 3913

#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int main()
{
	int t,flag;char a[1000000];
	stack<char> g;
	scanf("%d",&t);
	while(t--)
	{
		while(!g.empty())
		{
			g.pop();
		}
		scanf("%s",a);
		flag=1;
		for(int i=0;i<strlen(a);i++)
		{
			if(a[i]=='(')
			{
				g.push(a[i]);
			}
			else if(a[i]==')')
			{
				if(g.empty())
				{
					flag=0;
					break;
				}
				else if(g.top()=='(')
				{
					g.pop();
				}	
			}
		}
		if(g.empty()&&flag==1)
		{
			printf("Yes\n"); 
		}
		else 
		{
			printf("No\n");
		}
	}
}

Double click to view unformatted code.


Back to problem 3913