canwen @ 2024-08-18 13:54:01
#include<bits/stdc++.h>
using namespace std;
struct node{
char a;
int id;
};
int pd[101];//为 1 为匹配上了
stack <node> st;
string s;
int main(){
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]=='('||s[i]=='['){
st.push((node){s[i],i});
}
else{
if(s[i]==')'){
if(!st.empty()&&st.top().a=='('){
node tmp=st.top();
pd[tmp.id]=pd[i]=1;
st.pop();
}else{
// st.push((node){s[i],i});
;
}
}else{
if(!st.empty()&&st.top().a=='['){
node tmp=st.top();
pd[tmp.id]=pd[i]=1;
st.pop();
}else{
// st.push((node){s[i],i});
;
}
}
}
}
for(int i=0;i<s.size();i++){
if(!pd[i]){
if(s[i]=='('||s[i]==')') cout<<"()";
if(s[i]=='['||s[i]=='[') cout<<"[]";
}else{
cout<<s[i];
}
}
return 0;
}
rt,只有
by YWHHDJSer @ 2024-08-18 14:04:18
([)
by rnf5114 @ 2024-08-18 14:06:09
@canwen
if(s[i]=='['||s[i]=='[') cout<<"[]";
by YWHHDJSer @ 2024-08-18 14:08:22
大概就是:栈只压左括号,如果当前是一个右括号,那么栈顶左括号必须与其匹配。
by canwen @ 2024-08-18 14:09:23
@rnf5114 谢谢大佬,我眼瞎了()
by canwen @ 2024-08-18 14:09:50
@YWHHDJSer 已通过,谢谢awa
by canwen @ 2024-08-18 14:10:18
此帖结