#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
bool cheak(string s){
stack<char> st;
for (int i = 0; i < s.size();i++){
if(s[i]=='['||s[i]=='('){
st.push(s[i]);
}else if(s[i]==']'){
if(st.empty()||st.top()!='[')
return 0;
st.pop();
}else if(s[i]==')'){
if (st.empty() || st.top() != '(')
return 0;
st.pop();
}
}
return st.empty();
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
string s;
cin >> s;
if(cheak(s))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
这道题我个人感觉是练习栈stack的好题!多写几遍可以提升对stack的理解与熟练度.

京公网安备 11010502036488号