92分求助,#8WA了

P1241 括号序列

DreamStar0628 @ 2024-01-28 00:53:33

#include<bits/stdc++.h>
using namespace std;
const int N=105;
char c[N],a[N];
string s;
int top=-1;
int main()
{
    int i,j;
    cin>>s;
    int n=s.length();
    for(i=0;i<n;i++)
    {
        int flag=1;
        if(s[i]==')')
        {
            for(j=top;j>=0;j--)
            {
                if(c[j]=='('&&a[j]==0)
                {
                    a[j]=1;
                    top++;
                    c[top]=')';
                    a[top]=1;
                    flag=0;
                    break;
                }
                else if(c[j]=='['&&a[j]==0)
                {
                    top++;
                    a[top]=1;
                    c[top]='(';
                    top++;
                    a[top]=1;
                    c[top]=')';
                    flag=0;
                    break;
                }
            }
            if(flag)
            {
                top++;
                c[top]=s[i];
            }
        }
        else if(s[i]==']')
        {
            for(j=top;j>=0;j--)
            {
                if(c[j]=='['&&a[j]==0)
                {
                    flag=0;
                    a[j]=1;
                    top++;
                    c[top]=']';
                    a[top]=1;
                    break;
                }
                else if(c[j]=='('&&a[j]==0)
                {
                    flag=0;
                    top++;
                    a[top]=1;
                    c[top]='[';
                    top++;
                    a[top]=1;
                    c[top]=']';
                    break;
                }
            }
            if(flag)
            {
                top++;
                c[top]=s[i];
            }
        }
        else
        {
            top++;
            c[top]=s[i];
        }
    }
    for(i=0;i<=top;i++)
    {
        if(c[i]=='('&&a[i]==0)
        {
            cout<<"()";
            continue;
        }
        else if(c[i]=='['&&a[i]==0)
        {
            cout<<"[]";
            continue;
        }
        else if(c[i]==']'&&a[i]==0)
        {
            cout<<"[]";
            continue;
        }
        else if(c[i]==')'&&a[i]==0)
        {
            cout<<"()";
            continue;
        }
        else cout<<c[i];
    }
    return 0;
}

by duzichen @ 2024-02-02 09:43:53

数组小了


by DreamStar0628 @ 2024-02-03 22:58:27

@duzichen 感谢感谢!!!


by duzichen @ 2024-02-04 10:48:30

没事,没事,我也是来水水讨论区的。


|