View Code of Problem 3913

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main() {
	char stack[1000000];
	int top=-1;
	int n;
	char a[1000000];
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",a);
		int len=strlen(a);
		int i,flag=0;
		if(a[0]==')')
		{
			flag=1;
		}
		for(i=0;i<len;i++)
		{
			if(a[i]=='(')
			{
				stack[++top]=a[i];
			}
			else if(a[i]==')')
			{
				if(top!=-1)
				{
					top--;
				}
				else
				{
					flag=1;
					break;
				}
			}
		}
		if(top==-1&&flag==0)
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
		
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3913