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
括号是就近匹配的,所以可以用栈来模拟,所以可以将括号压栈,匹配后出栈,最后栈底剩余的就是不能出栈的就是不能匹配的,一般的方法是找到这些括号但是太费劲了,我们同时建立一个栈,同时入栈,出栈,存括号的下标,那么在出栈操作之后,第一个stack就只剩下不匹配的括号,第二个stack就只剩下不匹配的括号的下标。
下标将括号数组分成了好几段,枚举每一段的左中括号的数量即可,比较最大值更新左右段点即可

#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
int b[maxn]={0};
stack<int>demo;
stack<int>de;
vector<int>ans;
string a;
int main()
{while(!demo.empty())demo.pop();while(!de.empty())de.pop();ans.clear(); //初始化操作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(demo.empty()||b[i]==-1||b[i]==-2){demo.push(b[i]);de.push(i);}else if(b[i]+demo.top()==0){demo.pop();de.pop();}else{demo.push(a[i]);de.push(i);}}if(demo.empty()){int res=0;for(int i=0;i<a.size();i++) if(a[i]=='[') res++;cout<<res<<endl<<a<<endl;return 0;}while(!de.empty()){ans.push_back(de.top());de.pop();}ans.push_back(n);//补足区间sort(ans.begin(),ans.end());int l,r=-1, /*补足区间*/ml,mr,res=0,maxi=0;for(int i=0;i<ans.size();i++){l=r+1;r=ans[i];res=0;for(int i=l;i<r;i++){if(a[i]=='[') res++;}if(res>maxi){ml=l;mr=r-1;maxi=res;}}if(maxi==0){cout<<0<<endl;return 0;}cout<<maxi<<endl;for(int i=ml;i<=mr;i++)  cout<<a[i];cout<<endl;
}

CodeForces - 224C. Bracket Sequence (栈模拟)简单做法相关推荐

  1. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  2. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  3. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  4. CCF 201903-2 二十四点 用栈模拟简单计算器

    二十四点 来源:CCF 背景 二十四点是一款著名的纸牌游戏,其游戏的目标是使用 3 个加减乘除运算使得 4张纸牌上数字的运算结果为 24. 题目 定义每一个游戏由 4 个从 1-9 的数字和 3 个四 ...

  5. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  6. Codeforces 26B. Regular Bracket Sequence

    Codeforces 26B. Regular Bracket Sequence 传送门:https://codeforces.com/problemset/problem/26/B 题目大意: 其实 ...

  7. 用C语言模拟栈(简单实现)

    //C语言模拟栈,简单实现 #include <stdio.h> #include <stdlib.h> typedef struct Node node; struct No ...

  8. Codeforces 926E - Merge Equal Elements(栈 + 模拟)

    You are given a sequence of positive integers a 1, a 2, -, a n. While possible, you perform the foll ...

  9. 【CodeForces】CF26B Regular Bracket Sequence

    题目地址: https://www.luogu.com.cn/problem/CF26B 题面翻译: 给出一个括号的序列,求最长的合法的子序列并输出. 原序列的长度≤106\le 10^6≤106 题 ...

最新文章

  1. 201671010145 2016-2017《Java程序设计》JAVA语言中,异常处理有哪几种方式?
  2. 计算机三级网络技术打印,全国计算机等级考试三级网络技术历年真题(整理_打印版)...
  3. 微信公众平台开发之在网页上添加分享到朋友圈,关注微信号等按钮
  4. 莱洛三角形和定宽曲线
  5. 单链表的读取、插入与删除
  6. sql like 通配符_《SQL必知必会》学习笔记
  7. G-sensor 介绍
  8. 程序员自己的商业模式
  9. 【网络】广域网、局域网、城域网
  10. PDF转图片的工具汇总
  11. agx 安装ros opencv_ROS基础
  12. @TOM VIP邮箱,打造商务办公新场景,定位职场人的贴心助手!
  13. 平台经济中国案例研究——网易云平台
  14. 程序员“薪资被应届生倒挂“现象明显,跳槽还是等待?
  15. JavaWeb学习笔记(HTML语言)
  16. 计算机有效策略无法连接打印机,共享打印提示:Windows无法连接到打印机,拒绝访问...
  17. 雅思词汇之万词plan:第1-365天
  18. 2021年6月7日08点37分 渗透测试基础部分
  19. 【经典之作】松岩老师打底十二法!前言
  20. 为魅族M8手机开发的围棋打谱软件(M8WeiqiPu)发布0.1版

热门文章

  1. htcvr设备计算机配置,准备买HTC VIVE了?来测试一下你的电脑配置够不够
  2. c语言作业题五六章答案,C语言程序设计五六章习题和课堂测试答案.doc
  3. 2. with check option能起什么作用?_家装要选择第三方装修监理吗?为什么?
  4. SpringMVC Root WebApplicationContext启动流程
  5. 某虚拟化项目总结:一条光纤引发的故障
  6. oc32--构造方法1
  7. CISCO ACL配置全解
  8. rtsp协议_Chromium(3/5):rtsp客户端
  9. php过去mysql数据表是空_PHP向mysql中写数据,在phpmyadmin中为空,直接打印有数据?...
  10. java获取0点的时间戳_Java获取凌晨时间戳的方法分析