32pts 求调
查看原帖
32pts 求调
678175
jqQt0220楼主2023/10/3 19:11

rt,记录

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int f[1010];
string c;
int main()
{
    cin>>c;
    int l=c.length();
    for(int i=0;i<l;i++)
    {
        if(c[i]==')'||c[i]==']')
        {
            while(!s.empty()&&c[s.top()]!='('&&c[s.top()]!='[')
                s.pop();
            if(c[i]==')'&&c[s.top()]=='('||c[i]==']'&&c[s.top()]=='[')
                f[i]=f[s.top()]=1;
        }
        else
        {
            s.push(i);
        }
    }
    for(int i=0;i<l;i++)
    {
        if(f[i])
            printf("%c",c[i]);
        else
        {
            if(c[i]=='('||c[i]==')')
                printf("()");
            if(c[i]=='['||c[i]==']')
                printf("[]");
        }
    }
    return 0;
}
2023/10/3 19:11
加载中...