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就只剩下不匹配的括号的下标。

下标将括号数组分成了好几段,枚举每一段的左中括号的数量即可,比较最大值更新左右段点即可

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. template <typename t>
  4. void read(t &x)
  5. {
  6. char ch = getchar();
  7. x = 0;
  8. int f = 1;
  9. while (ch < '0' || ch > '9')
  10. f = (ch == '-' ? -1 : f), ch = getchar();
  11. while (ch >= '0' && ch <= '9')
  12. x = x * 10 + ch - '0', ch = getchar();
  13. x *f;
  14. }
  15. #define wi(n) printf("%d ", n)
  16. #define wl(n) printf("%lld ", n)
  17. #define P puts(" ")
  18. typedef long long ll;
  19. #define MOD 1000000007
  20. #define mp(a, b) make_pair(a, b)
  21. //---------------https://lunatic.blog.csdn.net/-------------------//
  22. const int N = 1e5 + 5;
  23. char a[N];
  24. stack<char> m;
  25. stack<int> n;
  26. vector<int> x;
  27. int main()
  28. {
  29. scanf("%s", a);
  30. int ln = strlen(a);
  31. int cnt = 0;
  32. //cout<<ln<<endl;
  33. for (int i = 0; i < ln; i++)
  34. {
  35. if (!m.size() || a[i] == '(' || a[i] == '[')
  36. {
  37. m.push(a[i]);
  38. n.push(i);
  39. continue;
  40. }
  41. else if (a[i] == ')' && m.top() == '(')
  42. {
  43. n.pop();
  44. m.pop();
  45. //cout << 1 << endl;
  46. }
  47. else if (a[i] == ']' && m.top() == '[')
  48. {
  49. m.pop();
  50. n.pop();
  51. cnt++;
  52. // cout << 2 << endl;
  53. }
  54. else
  55. {
  56. m.push(a[i]);
  57. n.push(i);
  58. }
  59. }
  60. // cout<<1<<endl;
  61. if (m.empty())
  62. {
  63. wi(cnt);
  64. P;
  65. printf("%s\n", a);
  66. }
  67. else
  68. {
  69. x.push_back(ln);
  70. while (!n.empty())
  71. {
  72. x.push_back(n.top());
  73. n.pop();
  74. }
  75. x.push_back(-1);
  76. int ml, mr, maxi = 0;
  77. cnt = 0;
  78. for (int i = x.size() - 1; i > 0; i--)
  79. {
  80. maxi = 0;
  81. // cout << x[i] + 1 << " " << x[i - 1] - 1 << endl;
  82. for (int j = x[i] + 1; j < x[i - 1]; j++)
  83. {
  84. if (a[j] == '[')
  85. maxi++;
  86. }
  87. if (maxi > cnt)
  88. {
  89. cnt = maxi;
  90. ml = x[i] + 1;
  91. mr = x[i - 1] - 1;
  92. }
  93. }
  94. wi(cnt);
  95. P;
  96. if (cnt == 0)
  97. return 0;
  98. for (int i = ml; i <= mr; i++)
  99. {
  100. putchar(a[i]);
  101. }
  102. P;
  103. }
  104. //P;
  105. }

CF思维联系–CodeForces -224C - Bracket Sequence的更多相关文章

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

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

  2. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. SpringMVC(一):简介和第一个程序

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  2. Spring(一):Spring入门程序和IoC初步理解

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  3. python3的subprocess的各个方法的区别(-)

    subprocess(python3.7) subprocess 主要是为了替换一下的模块函数,允许你执行一些命令,并获取返回的状态码和 输入,输出和错误信息. os.systemos.spawn* ...

  4. JAVA中Calendar 类的应用

    转自:https://www.imooc.com/code/2340 侵删! Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法 ...

  5. jsonpath 字典中取值

    jsonpath 第三方模块 def getsign(): url="http://api.nnzhp.cn/api/user/login" data = {"usern ...

  6. Python设计模式(11)-状态模式

    # coding=utf-8 # *状态模式:一个方法的判断逻辑太长,就不容易修改.方法过长,其本质就是,# * 就是本类在不同条件下的状态转移.状态模式,就是将这些判断分开到各个能# * 表示当前状 ...

  7. stand up meeting 12/8/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云  --------------    --  -----------  -- PDF Reader 朱玉影         ...

  8. VXLAN 基础教程:在 Linux 上配置 VXLAN 网络

    上篇文章结尾提到 Linux 是支持 VXLAN 的,我们可以使用 Linux 搭建基于 VXLAN 的 overlay 网络,以此来加深对 VXLAN 的理解,毕竟光说不练假把式. 1. 点对点的 ...

  9. Python - Python算法之冒泡算法的超简单实现

    [原创]转载请注明作者Johnthegreat和本文链接 冒泡排序在算法中算是最简单也最容易实现的,这里介绍一个非常简单实现的代码: def bubble_sort(ls): for first in ...

  10. Django文档阅读-Day3

    Django文档阅读-Day3 Writing your first Django app, part 3 Overview A view is a "type" of Web p ...