括号配对问题
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
程序如下:
#include<stdio.h>
#include<string.h>
void test(char a[5000]){
char z[5000];
char *p=a;
int m=0;
for(int i=0;i<strlen(a);i++,p++){
if(*p=='('||*p=='['){
z[m]=*p;
m++;
}
else if(*p==')'||*p==']'){
m--;
if((z[m]=='['&&*p==']')||(z[m]=='('&&*p==')')){
}
else{
m=5;break;
}
}
}
if(m>0){
printf("No\n");
}
else if(m==0){
printf("Yes\n");
}
}
int main(){
char s[10000];
int n;
scanf("%d",&n);
getchar();
while(n--){
scanf("%s",s);
test(s);
}
}
浙公网安备 33010602011771号