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. Linux服务器架设篇,DHCP服务器的搭建

    学习之前,我们首先来看一个案例: 假如你是一个学校的网络管理老师,需要为教室的70多台电脑配置好网络你会怎么办? 一台一台的给他们配置? 在这里我特别欣赏鸟哥的一句话--"当管理员最大的幸福 ...

  2. 05-CSV文件读取(问题)

    1.支持.txt,.log,.json三种格式 并且也支持.csv格式文件类型----.csv在使用时会出现乱码情况 2.当时遇到问题: CSV文件.txt文本内有数据,如:user ,pwd adm ...

  3. tf.nn.relu6 激活函数

    tf.nn.relu6(features,name=None) 计算校正线性6:min(max(features, 0), 6) 参数: features:一个Tensor,类型为float,doub ...

  4. windows powershell校验下载的文件MD5和SHA1值

    Windows自带MD5 SHA1 SHA256命令行工具 certutil -hashfile <文件名> <hash类型> 打开windows powershell,进入到 ...

  5. AJ学IOS 之二维码学习,快速生成二维码

    AJ分享,必须精品 二维码是一项项目中可能会用到的,iOS打开相机索取二维码的速度可不是Android能比的...(Android扫描二维码要来回来回晃...) 简单不多说,如何把一段资料(网址呀,字 ...

  6. 【DataBase】更改root根用户密码 和 SQLyog安装

    更改root根用户密码 和 SQLyog安装 无密码登录MySQL mysql -u root -p 修改密码与更新加密规则 ALTER USER 'root'@'localhost' IDENTIF ...

  7. notepad++批量每行加字符

    移动光标到头 选择正则 输入^ 下面输入需要加的文本. 点替换

  8. Linux编辑利器-Vim

    在大学时代,Vim 的大名就已如雷贯耳,但由于它陡峭的学习曲线,一直望而却步.等真正开始学习之后,发现并没有想象中的复杂,也没有所谓的瓶颈,只要在实际写代码中强迫自己使用就可以了,无形中就会形成习惯. ...

  9. eclipse git 文件状态 及git分支的创建与合并与删除

    eclipse里面Git文件状态及图标展示   EGit会出现如下图标,其对应状态及意义如下:      1)忽略[ ignored ]:仓库认为该文件不存在(如bin目录,不需要关注).通过右键Te ...

  10. PHP反序列化漏洞总结

    写在前边 做了不少PHP反序列化的题了,是时候把坑给填上了.参考了一些大佬们的博客,自己再做一下总结 1.面向对象 2.PHP序列化和反序列化 3.PHP反序列化漏洞实例 1.面向对象 在了解序列化和 ...