#include <string.h>
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 0;
cin >> n;
while (n--)
{
char str[10005];
cin >> str;
int len = strlen(str);
char vec[10005];
int flag = 1;
int j=-1;
for (int i = 0; i < len; i++)
{
switch (str[i])
{
case '(':
case '[':
j++;
vec[j]=str[i];
break;
case ')':
case ']':
if (j<0)/*处理特殊情况*/
{
flag = 0;
}
else
{
if (int(vec[j]) == int(str[i]) - 1 || int(vec[j]) == int(str[i]) - 2)
{
vec[j] = NULL;
j--;
}
else
{
flag = 0;
}
}
break;
default:
break;
}
}
if (strlen(vec) != 0)
{
flag = 0;
}
if (flag==0)
{
cout << "No" << endl;
}
else
{
cout << "Yes" << endl;
}
}
return 0;
}
其实思路不难,各种情况要考虑到,想的往下写就OK
NYOJ 2 括号配对问题
最新推荐文章于 2018-07-20 20:48:34 发布