题目链接:
http://codeforces.com/problemset/problem/223/A
A. Bracket Sequence
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
Copy
([])output
Copy
1 ([])input
Copy
(((output
Copy
0
题意:给出一个括号序列,要求寻找这个序列的子串,要求这个子串是一个匹配的括号序列,而且其中包含 [] 的个数最多
思路:栈
利用栈对括号进行匹配,不断的入栈出栈,最后使得所有不匹配的括号位置都在栈中,然后分别对栈中不匹配的位置进行分段统计,统计序列中每一段匹配位置中 [] 的个数,最后记录个数最多的值
以 ([[]]([] 为例,将括号序列与序列长度依次压入栈中,将序列长度压入栈中是为了便于统计序列最后一段
有:
栈中元素 0 1 2 3 4 5 6 7 8(len) 对应含义 ( [ [ ] ] ( [ ] 序列长度 匹配完毕后:
栈中元素 0 5 8(len) 对应含义 ( ( 序列长度 每次出栈两个元素,分别统计原序列区间 [1,4]、[6,7] 匹配字段中 [] 的个数,更新最大值
This is the code:
#include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<iomanip> #include<list> #include<map> #include<queue> #include<sstream> #include<stack> #include<string> #include<set> #include<vector> using namespace std; #define PI acos(-1.0) #define pppp cout<<endl; #define EPS 1e-8 #define LL long long #define ULL unsigned long long //1844674407370955161 #define INT_INF 0x3f3f3f3f //1061109567 #define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399 // ios::sync_with_stdio(false); // 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。 const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1}; int read()//输入外挂 {int ret=0, flag=0;char ch;if((ch=getchar())=='-')flag=1;else if(ch>='0'&&ch<='9')ret = ch - '0';while((ch=getchar())>='0'&&ch<='9')ret=ret*10+(ch-'0');return flag ? -ret : ret; } const int maxn=500000+5; stack<int> S; int a[maxn]; void cal(string str) {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();elseS.push(i);} } int main() {string str;cin>>str;cal(str);int len=str.size();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; }