您的位置:首页 > 其它

NYOJ 2 括号配对问题

2015-11-11 21:11 281 查看
#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