简单的题目,模拟栈的输入和输出操作,有两个点容易忽略:第一,字符串当中可能存在空格,这个要注意;第二,空串也是合法的输入。具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
using namespace std;
int n;
int main(){
cin >> n;
string del;
getline(cin,del);
while (n--){
string s;
getline(cin, s);
if (s.empty()){
cout << "Yes" << endl;
continue;
}
stack<char> st;
bool flag = true;
for (int i = 0; i < s.size(); i++){
if (s[i] == ' ') continue;
if (s[i] == '('||s[i]=='['){
st.push(s[i]);
}
else if (s[i] == ')'){
if (st.empty() || st.top() != '('){
flag = false;
break;
}
else st.pop();
}
else{
if (st.empty() || st.top() != '['){
flag = false;
break;
}
else st.pop();
}
}
if (!flag){
cout << "No" << endl;
}
else{
if (st.empty()) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
return 0;
}