#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool is_valid(string s){
stack<char> stack;
for (int i = 0;i < s.length();i++){
if (s[i] == '(' || s[i] == '['){
stack.push(s[i]);
}
else {
if (stack.empty()) return false;
if ((stack.top() == '(' && s[i] == ')' ) ||
(stack.top() == '[' && s[i] == ']')) {
stack.pop();
}
else {
return false;
}
}
}
return stack.empty();
}
int main(){
int n;
cin >> n;
cin.get();
for (int i = 0;i < n;i++){
string s;
getline(cin,s);
if (is_valid(s)){
cout<<"Yes"<<endl;
}
else {
cout<<"No"<<endl;
}
}
return 0;
}
Parentheses Balance 括号匹配
最新推荐文章于 2024-09-02 13:43:09 发布