题解
2023-08-24 13:57:32
发布于:广东
10阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
bool chk(string s) {
stack<char> st;
for (char c : s) {
if (c == '(' || c == '[') {
st.push(c);
} else if (c == ')' || c == ']') {
if (st.empty()) {
return false;
}
char top = st.top();
st.pop();
if ((c == ')' && top != '(') || (c == ']' && top != '[')) {
return false;
}
}
}
return st.empty();
}
int main() {
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (chk(s)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
这里空空如也
有帮助,赞一个