Gooooogle • 6个月前
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string str;
int k;
int main() {
cin >> k;
for (int f = 1; f <= k; f++) {
cin >> str;
stack <int> s;
bool sign = true;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(' || str[i] == '{' || str[i] == '<' || str[i] == '[') {
int x;
if (s.size() >= 1)
x = s.top();
if (str[i] == '(')
s.push(2);
if (str[i] == '{')
s.push(4);
if (str[i] == '[')
s.push(3);
if (str[i] == '<')
s.push(1);
if (s.size() >= 2 && x < s.top()) {
sign = false;
//cout << "@";
//cout << "NO11" << endl;
break;
}
}
if (str[i] == ')' || str[i] == '}' || str[i] == '>' || str[i] == ']') {
if (s.empty()) {
sign = false;
//cout << "NO22" << endl;
break;
} else {
//printf("%c top=%d size=%d\n", str[i], s.top(), s.size());
if (str[i] == ')' && s.top() == 2)
s.pop();
else if (str[i] == '}' && s.top() == 4)
s.pop();
else if (str[i] == ']' && s.top() == 3)
s.pop();
else if (str[i] == '>' && s.top() == 1)
s.pop();
else {
sign = false;
}
}
}
}
if (s.empty() && sign) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
评论:
请先登录,才能进行评论