#include<bits/stdc++.h>
#include<string>
using namespace std;
int main(){
int flag=1;
stack<char> s;
stack<char,vector<char> > stk;
string str;
getline(cin,str);
for(int i=0;i<str.size();i++){
if(str[i]=='{'||str[i]=='['||str[i]=='('){
s.push(str[i]);
}
else if(str[i]=='}'){
if(!s.empty()){
if(s.top()!='{'){
cout<<"no";
flag=0;
break;
}
else s.pop();
}
else{
flag=0;
break;
}
}
else if(str[i]==']'){
if(!s.empty()) {
if(s.top()!='['){
cout<<"no";
flag=0;
break;
}
else s.pop();
}
else{
flag=0;
break;
}
}
else if(str[i]==')'){
if(!s.empty()) {
if(s.top()!='('){
cout<<"no";
flag=0;
break;
}
else s.pop();
}
else{
flag=0;
break;
}
}
}
if(flag==1){
if(s.empty()) cout<<"yes";
else cout<<"no";
}
return 0;
}
这题我以前做过,但我突然找不到代码了,我记得当时我用的是暴力还是搜索来着。就是从第一个开始循环,遇到匹配的都给他赋值为-1,然后循环时判断一下该位置是不是-1在找匹配,希望对你有帮助。如果有帮助的话,能采纳吗?