直接根据题目翻译,得到的代码只能得40分,其他是WA,有没有大佬帮帮忙,求求了
#include <bits/stdc++.h>
using namespace std;
int n,ans,cur;//n为字符串长度 ans为答案 cur为当前最大值
stack<int> st;//存放( [ {
string s;
int main()
{
cin>>s;
n=s.size();
for(int i=0;i<n;i++){
if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
st.push(s[i]);
}
else{
if(st.empty()){//空栈时更新一次最大值
ans=max(ans,cur);
cur=0;
while(!st.empty()) st.pop();
}
else{//符号条件时两两配对 cur+=2
if(s[i]==')' && st.top()=='('){
cur+=2;
st.pop();
}
else if(s[i]==']' && st.top()=='['){
cur+=2;
st.pop();
}
else if(s[i]=='}' && st.top()=='{'){
cur+=2;
st.pop();
}
else{
ans=max(ans,cur);
cur=0;
while(!st.empty()) st.pop();
}
}
}
}
cout<<ans;
return 0;
}