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. 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. spring的jdbc具名参数

    在jdbc的模板中使用具名参数: 1.就需要在之前的jdbc的例子中进行修改:需要在xml文件中重新配置一个bean.这是固定的格式.如下 对于使用具名参数而言.配置NamedParameterJdb ...

  2. MTK Android Android数据保存到系统数据库

    如果有留意Android中系统设置Settings里面的源码,你会发现代码中频繁用到了Settings.System操作,该类通过键值对的形式,将一些特定的值以全局的模式保存到Setting的数据库中 ...

  3. Unity - 旋转方法

    前言 本文梳理了Unity中常用的旋转方法,涉及两大类:Transform.Quaternion. Transform 类 Rotate() 此方法重载多,易理解,在连续动态旋转中较为常用. /* o ...

  4. Linux 磁盘管理篇(一 磁盘分区)

    显示系统所有分区内容            fdisk 分区工具                parted fdisk: 执行完后按下 q 是退出不保存操作的意思 执行完后按下 w 是执行操作的意思 ...

  5. 面试官求你了,别再问我TCP的三次握手和四次挥手

    少点代码,多点头发 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 三次握手建立链接,四次挥手断 ...

  6. 34.2 字节流 InputStreamReader OutputStreamWriter

    使用方法同字符流,不一样的是数据类型是字节 copydemo public static void main(String[] args) throws IOException { InputStre ...

  7. echarts多个数据添加多个纵坐标

    在我们echarts开发中,肯定会遇到一个问题.那就是当有多个数据且数据大小差距太大时,就会出现有些数据小到看不到的情况.所以在遇到这种情况时,我通常的解决办法就是给他多加一个坐标轴. option  ...

  8. Spring Data REST不完全指南(一)

    简介 Spring Data REST是Spring Data项目的一部分,可轻松在Spring Data存储库上构建超媒体驱动的REST Web服务. Spring Data REST 构建在 Sp ...

  9. asp.net core webapi Session 跨域

    在ajax 请求是也要加相应的东西 $.ajax({ url:url, //加上这句话 xhrFields: { withCredentials: true } success:function(re ...

  10. 通达OA任意用户登录 漏洞复现

    0x00 漏洞简介 通达OA国内常用的办公系统,使用群体,大小公司都可以,其此次安全更新修复的高危漏洞为任意用户登录漏洞.攻击者在远程且未经授权的情况下,通过利用此漏洞,可以直接以任意用户身份登录到系 ...