使用栈模拟题目,代码求调,感谢
  • 板块P10472 括号画家
  • 楼主IXgyy
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/9/21 21:18
  • 上次更新2024/9/21 21:57:01
查看原帖
使用栈模拟题目,代码求调,感谢
1088776
IXgyy楼主2024/9/21 21:18

直接根据题目翻译,得到的代码只能得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;
}
2024/9/21 21:18
加载中...