codeforces-223A-Bracket Sequence

从前往后进行括号匹配。

如果匹配到,就把当前位置,和被匹配的括号的位置标记为对应括号的位置。

然后一段连续的不带0的区域即时一个可用的子串。

然后从这些字串中寻找中括号最多的字串。

#include <stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<stack>
#define LL long long
using namespace std;
int a[200000];
int num[200000];
int b[200000];
int vis[200000];
stack<int>sta;
stack<int>stb;
char str[200001];
int main()
{
    int i,n,k;
    while(~scanf("%s",str))
    {
        memset(num,0,sizeof(num));
        memset(b,0,sizeof(b));
        int len=strlen(str);
        for(i=0; i<len; i++)
        {
            if(str[i]=='(')a[i+1]=1;
            if(str[i]=='[')
            {
                a[i+1]=2;
                num[i+1]++;
            }
            if(str[i]==')')a[i+1]=3;
            if(str[i]==']')a[i+1]=4;
            num[i+1]+=num[i];
        }
        while(!sta.empty())sta.pop();
        while(!stb.empty())stb.pop();
        for(i=1; i<=len; i++)
        {
            if(a[i]<=2)
            {
                sta.push(a[i]);
                stb.push(i);
            }
            else
            {
                if(sta.empty())
                {
                    continue;
                }
                int xx,yy;
                xx=sta.top();
                sta.pop();
                yy=stb.top();
                stb.pop();
                if(xx%2==a[i]%2)
                {
                    b[i]=yy;
                    b[yy]=i;
                }
                else
                {
                    while(!sta.empty())sta.pop();
                    while(!stb.empty())stb.pop();
                }
            }
        }
       /* for(i=1;i<=len;i++)
        {
            cout<<b[i]<<" ";
        }
        cout<<endl;*/
        int maxx=0;
        int ma=0;
        int st=-1;
        int ed=-1;
        int ss=1;
        for(i=1;i<=len+1;i++)
        {
            if(b[i]==0)
            {
                if(maxx<ma)
                {
                    maxx=ma;
                    ed=i-1;
                    st=ss;

                }
                ss=i+1;
                ma=0;
            }
            else
            {
                if(a[i]==2)ma++;
            }
        }
        cout<<maxx<<endl;
        if(maxx>0)
        {
            for(i=st;i<=ed;i++)
            {
                printf("%c",str[i-1]);
            }
            cout<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值