括号匹配问题(一)

 

括号配对问题

时间限制: 3000ms

内存限制: 128000KB

64位整型:      Java 类名:

题目描述

现在,有一行括号序列,请你检查这行括号是否配对。

输入

第一行输入一个数N(0

输出

每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No

样例输入

3
[(])
(])
([[]()])

样例输出

No
No
Yes

 

 

 

 

用数组模拟栈

 

 

 

#include
#include
char s[20003];
int main()
{
	int t;
	scanf("%d",&t);
	getchar();
	while(t--){
		int top=0;
		char ch;
		while((ch=getchar())!='\n'){
			if(ch==')' && top>0 && s[top-1]=='(')
				top--;
			else if(ch==']' && top>0 && s[top-1]=='[')
				top--;
			else	s[top++]=ch;
		}
		if(top==0)	printf("Yes\n");
		else	printf("No\n");
	}
	return 0;
}

 

 

 

 

 

 

 

你可能感兴趣的:(二叉树和栈)