救 70

P1241 括号序列

fanersaiwenxue @ 2021-07-23 16:52:45

70 真的看不懂题

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[105];
int main()
{
    int i,j;
    char s[10001];
    cin>>s;
    for(i=0;i<strlen(s);i++)
    {
        if(s[i]==')')
        {
            for(j=i-1;j>=0;j--)
            {
                if(s[j]=='(' and a[j]==0)
                {
                    a[i]=a[j]=1;
                    break;
                }
                else if(s[j]==']' and a[j]==0)
                break;
            }
        }
        else if(s[i]==']')
        {
            for(j=i-1;j>=0;j--)
            {
                if(s[j]=='[' and a[j]==0)
                {
                    a[i]=a[j]=1;
                    break;
                }
                else if(s[j]=='(' and a[j]==0)
                break;
            }
        }
    }
    for(i=0;i<strlen(s);i++)
    {
        if(a[i]==0)
        {
            if(s[i]=='(' or s[i]==')')
            {
                cout<<"()";
            }
            else cout<<"[]";
        }
        else cout<<s[i];
    }
    return 0;
}

by fanersaiwenxue @ 2021-07-23 16:55:11

@zuiwancheng


by fanersaiwenxue @ 2021-07-23 16:56:00

@§Chtholly§


by fanersaiwenxue @ 2021-07-23 17:01:25

救救孩子吧


by Chtholly_L @ 2021-07-23 17:05:39

你这代码如果输入([)]就不行


by fanersaiwenxue @ 2021-07-23 17:08:53

是啊


by fanersaiwenxue @ 2021-07-23 17:09:11

那该怎么办呢


by cscst @ 2021-08-16 10:29:17

这不是题解的代码吗???


by cscst @ 2021-08-16 10:37:20

@fanersaiwenxue 你的一个中括号写反了 应该是这样的:

else if(s[j]=='[' && a[j]==0)
break;

这个是判断s[i] == ')' 的if里,第二重for中的else语句


|