ACM思维题训练集合
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;
template <typename t>
void read(t &x)
{char ch = getchar();x = 0;int f = 1;while (ch < '0' || ch > '9')f = (ch == '-' ? -1 : f), ch = getchar();while (ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();x *f;
}
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
//---------------https://lunatic.blog.csdn.net/-------------------//
const int N = 1e5 + 5;
char a[N];
stack<char> m;
stack<int> n;
vector<int> x;
int main()
{scanf("%s", a);int ln = strlen(a);int cnt = 0;//cout<<ln<<endl;for (int i = 0; i < ln; i++){if (!m.size() || a[i] == '(' || a[i] == '['){m.push(a[i]);n.push(i);continue;}else if (a[i] == ')' && m.top() == '('){n.pop();m.pop();//cout << 1 << endl;}else if (a[i] == ']' && m.top() == '['){m.pop();n.pop();cnt++;// cout << 2 << endl;}else{m.push(a[i]);n.push(i);}}// cout<<1<<endl;if (m.empty()){wi(cnt);P;printf("%s\n", a);}else{x.push_back(ln);while (!n.empty()){x.push_back(n.top());n.pop();}x.push_back(-1);int ml, mr, maxi = 0;cnt = 0;for (int i = x.size() - 1; i > 0; i--){maxi = 0;//  cout << x[i] + 1 << " " << x[i - 1] - 1 << endl;for (int j = x[i] + 1; j < x[i - 1]; j++){if (a[j] == '[')maxi++;}if (maxi > cnt){cnt = maxi;ml = x[i] + 1;mr = x[i - 1] - 1;}}wi(cnt);P;if (cnt == 0)return 0;for (int i = ml; i <= mr; i++){putchar(a[i]);}P;}//P;
}

CF思维联系–CodeForces -224C - Bracket Sequence相关推荐

  1. CodeForces - 224C. Bracket Sequence (栈模拟)简单做法

    A bracket sequence is a string, containing only characters "(", ")", "[&quo ...

  2. 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)

    题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 1 #include <bits/s ...

  3. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  4. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  5. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  6. CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)

    ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...

  7. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  8. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  9. 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 ...

最新文章

  1. 简述RHEL7新特性(一)
  2. thinkphp去掉index.php
  3. python3.7打包exe坑_[求助]入坑学习python 需要装pyinstaller打包成exe
  4. 160 - 8 Andrnalin.1
  5. pytorch中Mini-batch批处理实现函数xx.unsqueeze(0)
  6. Linux db2 4499,db2 連接 ERRORCODE=-4499, SQLSTATE=08001,解決辦法
  7. 51单片机冒泡排序_51单片机片外冒泡排序
  8. 阿里研究院:解读互联网经济十大议题
  9. css中表居中,CSS DIV中表格居中显示
  10. 帆软:不使用 __parameters__ 传参,问题。
  11. 【中学】寻找阿姆斯特朗数
  12. 弹性法计算方法的mck法_经济学原理中讲到的中点法计算需求弹性是怎么回事
  13. java时间日期相减得到天数_java日期相减得到天数
  14. 编译原理 实验二 递归下降语法分析程序
  15. 《计算机网络(第7版)-谢希仁》期末复习
  16. c语言实验报告 订票系统,【C语言】火车订票系统
  17. 红尘梦落,卧醉千年,当所有的繁华散尽
  18. 什么是Rootkit病毒
  19. 低碳生活进行时!国产“芯”RK3568创造智慧出行新体验
  20. 手机双摄像头原理及产业解析----转载

热门文章

  1. Fragment切换。radiobutton加fragment切换(附件源码下载)
  2. NSNotification、delegate和KVO的区别
  3. Spring系列教程八: Spring实现事务的两种方式
  4. TCP报文发送的那些事
  5. 微信小程序走出国门,国际化将指日可待?
  6. 想靠大数据创业 你需要了解什么
  7. 今天学习啦所谓的高级语言啦
  8. 【Windows7系统新特性】
  9. php求及格,详解PHP通过递归实现提成计算
  10. c语言入门程序下载,简单实用——C语言入门程序练习