C真括号匹配

You are given a bracket sequence ss of length nn, You need to determine whether the given string is a regular bracket sequence.

Recall what the regular bracket sequence is:

  • "()" is regular bracket sequence;
  • if ss is regular bracket sequence then "(" + ss + ")" is regular bracket sequence;
  • if ss and tt are regular bracket sequences then ss + tt is regular bracket sequence.

For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.

Input

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (1≤n≤2⋅10^51≤n≤2⋅105) — the length of ss. The second line of the test case containg the string ss.

Output

For each test case, print YES if the given string is a regular bracket sequence, or NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

Sample 1

InputcopyOutputcopy
2
6
(()())
4
)()(
YES
NO

 

#include<stdio.h>
#include<string.h>
#define length 100

char *top;
char push(char bracket){
	top=top+1;
	*(top)=bracket;
}
void pop()
{
	top=top-2;
}
int main()
{   int n;
	scanf("%d",&n);
	while(n--){
		int l=0;
		scanf("%d",&l);
		
		char array[l];
		char stack[l];
		scanf("%s",array);
		
		int stack_length=strlen(array);
		top=stack-1;
		for(int i=0;i<stack_length;i++){
			push(array[i]);		    //每一个步骤都先把数组里的括号压入栈里 
			if(array[i] == ']'){	//如果压入的是 ] 号, 
				if(*(top-1) == '['){	//就在当前位置往下一个单位判断有没有 [ 号,如果有,就弹出栈 
					pop();
				}
			}
			if(array[i] == ')'){
				if(*(top-1) == '('){
					pop();
				}
			}
			if(array[i] == '}'){
				if(*(top-1) == '{'){
					pop();
				}
			}
		}		
		if(top == stack-1){
			printf("YES\n"); 
		}
		else{
			printf("NO\n");
		}
	
	}
		
		
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是你的奥奥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值