输入:
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