52 剩下几个数据re了咋回事求助

P1241 括号序列

jiaotang @ 2024-07-23 09:37:44

#include <bits/stdc++.h>
using namespace std;

string a;
stack<int> st;
bool vis[505];

int main()
{
    cin>>a;
    for(int i=0;i<a.size();i++)
    {
        if(a[i]=='('||a[i]=='[') st.push(i);
        else if(a[i]==')'&&a[st.top()]=='(')
        {
            vis[st.top()]=1;
            vis[i]=1;
            st.pop();
        }
        else if(a[i]==']'&&a[st.top()]=='[')
        {
            vis[st.top()]=1;
            vis[i]=1;
            st.pop();
        }
    }
    for(int i=0;i<a.size();i++)
    {
        if(vis[i]==1) cout<<a[i];
        else
        {
            if(a[i]=='('||a[i]==')') cout<<"()";
            else cout<<"[]";
        }
    }
    return 0;
}

by jiaotang @ 2024-07-23 09:40:37

已解决


by jiaotang @ 2024-07-23 09:40:59

#include <bits/stdc++.h>
using namespace std;

string a;
stack<int> st;
bool vis[505];

int main()
{
    cin>>a;
    for(int i=0;i<a.size();i++)
    {
        if(a[i]=='('||a[i]=='[') st.push(i);
        else if(a[i]==')'&&!st.empty()&&a[st.top()]=='(')
        {
            vis[st.top()]=1;
            vis[i]=1;
            st.pop();
        }
        else if(a[i]==']'&&!st.empty()&&a[st.top()]=='[')
        {
            vis[st.top()]=1;
            vis[i]=1;
            st.pop();
        }
    }
    for(int i=0;i<a.size();i++)
    {
        if(vis[i]==1) cout<<a[i];
        else
        {
            if(a[i]=='('||a[i]==')') cout<<"()";
            else cout<<"[]";
        }
    }
    return 0;
}

|