
#include <stdio.h>
// 检查括号匹配是否合法
int checkBracket(char * s,int size)
{
char op[100];
char opLeft[10] = {'(','['};
char opRight[10] = {')',']'};
int tt = 0;
for(int i = 0;i < size;i++)
{
char c = s[i];
// 左括号全部入栈
if(c == opLeft[0] || c == opLeft[1])op[++tt] = s[i];
// 右括号
else if(c == opRight[0] || c == opRight[1])
{
//匹配到右括号,但没有左括号说明不合法
if(tt == 0)return 0;
// preOp 栈顶符号
char preOp = op[tt--];// 出栈
// 左右括号不匹配的状态 (] [)
if((preOp == opLeft[0] && c == opRight[1]) || (preOp == opLeft[1] && c == opRight[0])) return 0;
}
}
// 所有括号都出栈
return tt == 0;
}
int main() {
// 测试样例 Y N Y N N N
char str[][10] = {"([]())","))))","[([][])]","[(])","((])","((((((",};
int size[10] = {6,4,8,4,4,6};
for (int i = 0; i < 6; ++i) {
if(checkBracket(str[i],size[i]))puts("Yes");
else puts("No");
}
return 0;
}