题意:
检测括号是否匹配,注意有空格
#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<math.h>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
struct Node
{
int r;
int c;
int total;
};
const Node dir[] = { { -1, 2 }, { 1, 2 }, { -2, 1 }, { 2, 1 }, { -2, -1 }, { 2,
-1 }, { -1, -2 }, { 1, -2 } };
int main()
{
string yes = "Yes";
string no = "No";
int n;
cin >> n;
string str;
getline(cin, str);
while (n--)
{
getline(cin, str);
int length = str.length();
stack<char> s;
int ok = 1;
for (int i = 0; i < length; i++)
{
char c = str.at(i);
if (c == ' ')
continue;
if (c == '(' || c == '[')
{
s.push(c);
}
else if (c == ')' || c == ']')
{
if (s.size() == 0)
{
ok = 0;
break;
}
else
{
char cc = s.top();
s.pop();
if (c == ')')
{
if (cc != '(')
{
ok = 0;
break;
}
}
else
{
if (cc != '[')
{
ok = 0;
break;
}
}
}
}
}
if (s.size() != 0)
ok = 0;
if (ok)
cout << yes << endl;
else
cout << no << endl;
}
}
浙公网安备 33010602011771号