首页 > 其他 > 详细

栈之括号匹配

时间:2014-09-06 17:25:13      阅读:278      评论:0      收藏:0      [点我收藏+]
输入:
3
[(])
(])
([[]()])
样例输出
No
No
Yes


#include<cstdio>
#include<cstring>
char str[1010];
char stack[1010];
int main()
{
	int T;
	while(~scanf("%d",&T)) 
	{
		while(T--)
		{
			scanf("%s",str);
			int top=1;
			stack[top++]=str[0];
			for(int i=1;i<strlen(str);i++)
			{
				if(str[i]==']' && stack[top-1]=='[')
				     top--;
				else if(str[i]==')' && stack[top-1]=='(')
				     top--;
				else
				     stack[top++]=str[i];
			}
			  printf( top==1 ? "Yes\n" : "No\n");
		}
	}
	return 0;
} 

栈之括号匹配

原文:http://blog.csdn.net/holyang_1013197377/article/details/39102311

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!