CodeForces - 224C:Bracket Sequence(STL,栈)

Discription
A bracket sequence is a string, containing only characters “(”, “)”, “[” and “]”.

A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters “1” and “+” between the original characters of the sequence. For example, bracket sequences “()[]”, “([])” are correct (the resulting expressions are: “(1)+[1]”, “([1+1]+1)”), and “](” and “[” are not. The empty string is a correct bracket sequence by definition.

A substring s[l… r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2… s|s| (where |s| is the length of string s) is the string slsl + 1… sr. The empty string is a substring of any string by definition.

You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets «[» as possible.

Input
The first and the only line contains the bracket sequence as a string, consisting only of characters “(”, “)”, “[” and “]”. It is guaranteed that the string is non-empty and its length doesn’t exceed 105 characters.

Output
In the first line print a single integer — the number of brackets «[» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them.

Examples
Input
([])
Output
1
([])
Input
(((
Output
0

题意
给定一个字符串,找出所包含匹配的[]最多的最长子串。

思路
用栈模拟,遇到“(”或者“[”就入栈,遇到“)”或者“]”就将和它匹配的左括号出栈,下表和字符同时操作,用一个vector储存所有不匹配的下标,排序,遍历每一段子区间,求得含[]最多的字符船的初末位置,然后遍历输出。

AC代码

#include <bits/stdc++.h>
using namespace std;
int b[100010]={0};
stack<int>ch;
stack<int>sta;
vector<int>v;
string a;
int main()
{
    cin>>a;
    int n=a.size();
    for(int i=0;i<a.size();i++)
    {
        if(a[i]=='(') b[i]=-2;
        else  if(a[i]==')') b[i]=2;
        else if(a[i]=='[') b[i]=-1;
        else b[i]=1;
    }
    for(int i=0;i<n;i++){
        if(ch.empty()||b[i]==-1||b[i]==-2)
        {
            ch.push(b[i]);
            sta.push(i);
        }
        else if(b[i]+ch.top()==0)
        {
            ch.pop();
            sta.pop();
        }
    else{
            ch.push(a[i]);
            sta.push(i);
        }
    }
    if(ch.empty())
    {
        int res=0;
        for(int i=0;i<a.size();i++) if(a[i]=='[') res++;
        cout<<res<<endl<<a<<endl;
        return 0;
    }
    while(!sta.empty())
    {
        v.push_back(sta.top());
        sta.pop();
    }
    v.push_back(n);
    sort(v.begin(),v.end());
    int l,r=-1,ml,mr,res=0,maxx=0;
     for(int i=0;i<v.size();i++)
    {
        l=r+1;
        r=v[i];
        res=0;
        for(int i=l;i<r;i++)
        {
            if(a[i]=='[') res++;
        }
        if(res>maxx)
        {
            ml=l;mr=r-1;
            maxx=res;
        }
    }
    if(maxx==0){
        cout<<0<<endl;
        return 0;
    }
    cout<<maxx<<endl;
    for(int i=ml;i<=mr;i++)
        cout<<a[i];
    cout<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值