Bracket Sequence(CF-223A)

Problem Description

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

题意:给出一个括号序列,要求寻找这个序列的子串,要求这个子串是一个匹配的括号序列,而且其中包含 [] 的个数最多

思路:栈

利用栈对括号进行匹配,不断的入栈出栈,最后使得所有不匹配的括号位置都在栈中,然后分别对栈中不匹配的位置进行分段统计,统计序列中每一段匹配位置中 [] 的个数,最后记录个数最多的值

以 ([[]]([] 为例,将括号序列与序列长度依次压入栈中,将序列长度压入栈中是为了便于统计序列最后一段

有:

栈中元素 0 1 2 3 4 5 6 7 8(len)
对应含义 ( [ [ ] ] ( [ ] 序列长度

匹配完毕后:

栈中元素 0 5 8(len)
对应含义 ( ( 序列长度

每次出栈两个元素,分别统计原序列区间 [1,4]、[6,7] 匹配字段中 [] 的个数,更新最大值

Source Program

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

stack S;
int a[N];
int main(){
    string str;
    cin>>str;
    int len=str.size();

    for(int i=0;ires){
            res=minn;
            start=left;
            endd=right;
        }
    }

    printf("%d\n",res);
    if(res!=0)
        for(int i=start;i<=endd;i++)
            cout<

 

你可能感兴趣的:(#,线性结构——栈与队列,#,CodeForces)