673 Parentheses Balance

Problem link: 673 – Parentheses Balance

 

 


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>

using namespace std;

int main()
{
	
	int n;
	
	cin >> n;
	cin.ignore();

	

	
	char s[1000],x[1000];
	
	for(int i=1;i<=n;i++){
		
		gets(s);

		int c,j,k,l;
		
		
		
		if(strcmp(s,"")!=0){
			l=strlen(s);
			k=0;
			for(j=0;j<l;j++){
				if((s[j]=='(') || (s[j]==')') || (s[j]=='[') || (s[j]==']')){
					x[k]=s[j];
					k++;
					
					}
					
			
			
			}
			x[k]='\0';
			strcpy(s,x);
		}
		if(strcmp(s,"")!=0){
			
			do{
				l=strlen(s);
				c=0;
				for(j=0;j<l-1;j++){
					
					if((s[j]=='(' && s[j+1]==')') || (s[j]=='[' && s[j+1]==']')){
						s[j]='1';
						s[j+1]='1';
						c++;
						j++;
						
						
						}
						
					}
				for(j=0,k=0;j<l;j++){
					if(s[j]!='1'){
						x[k]=s[j];
						k++;
						
						}
					
					}

					x[k]='\0';
					strcpy(s,x);
			
			}
			while(c!=0);
		
		}

		if(strcmp(s,"")==0)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
		
		}
		
	
	
	return 0;
}

Advertisements