通常,此问题主要用栈来进行,但在此通过数组,用数组的最后一位来模拟栈顶:梅毒如一个括号,若是左括号,则或者使置顶的最急迫的期待得以消解,或者是不合法的情况;若是左括号,则作为一个新的更急迫的期待压入栈中。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n,i,top=0;;
char ch[10000],s[5000];
cin >> n;
if (n<=0 || n>100)
return -1;
while (n--)
{
cin >> ch;
for (i=0;i<strlen(ch);i++)
{
if (ch[i] == '[' || ch[i] == '(')
s[top++]=ch[i];
else
{
if ((ch[i] == ')' && s[top-1] == '(') || (ch[i] ==']' && s[top-1] == '['))
top--;
else
{
cout << "No"<<endl;
break;
}
}
}
if (strlen(ch) != 0)
{
if (i==strlen(ch) && top==0)
cout << "Yes" <<endl;
}
else
n++;
top=0;
}
return 0;
}