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<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#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<int> S;
int a[N];
int main(){
string str;
cin>>str;
int len=str.size();
for(int i=0;i<len;i++){
if(str[i]=='[')
a[i]=-1;
else if(str[i]=='(')
a[i]=-2;
else if(str[i]==']')
a[i]=1;
else if(str[i]==')')
a[i]=2;
}
for(int i=0;i<len;i++){
if(S.empty()||a[i]==-1||a[i]==-2)
S.push(i);
else if((a[i]+a[S.top()])==0)
S.pop();
else
S.push(i);
}
int left,right;
S.push(len);
int res=0;
int start,endd;
while(!S.empty()){
if(S.size()!=1){
int temp=S.top();S.pop();
right=temp-1;
temp=S.top();
left=temp+1;
}
else{
int temp=S.top();S.pop();
right=temp-1;
left=0;
}
int minn=0;
for(int i=left;i<=right;i++)
if(str[i]=='[')
minn++;
if(minn>res){
res=minn;
start=left;
endd=right;
}
}
printf("%d\n",res);
if(res!=0)
for(int i=start;i<=endd;i++)
cout<<str[i];
return 0;
}