#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define N 110010 using namespace std; char a[N]; int sta[N]; int vis[N]; int ans,l,r; int main() { scanf("%s",a); int len=strlen(a); int tail=1; for(int i=0;i<len;i++) { if(tail==1) { sta[tail++]=i; vis[i]+=vis[i-1]; continue; } if(a[i]=='(') { sta[tail++]=i; } if(a[i]==')') { if(a[sta[tail-1]]=='(') tail--; else sta[tail++]=i; } if(a[i]=='[') { sta[tail++]=i; } if(a[i]==']') { if(a[sta[tail-1]]=='[') { tail--; vis[i]=1; } else sta[tail++]=i; } vis[i]+=vis[i-1]; } vis[len]+=vis[len-1]; if(tail==1) { cout<<vis[len]-vis[0]<<endl; cout<<a<<endl; return 0; } sta[tail++]=len; sta[0]=0; for(int i=1;i<tail;i++) { if(ans<vis[sta[i]]-vis[sta[i-1]]) { ans=vis[sta[i]]-vis[sta[i-1]]; if(i-1) l=sta[i-1]+1; else l=0; r=sta[i]; } } cout<<ans<<endl; for(int i=l;i<r;i++) cout<<a[i]; return 0; }
Codefroces 223A - Bracket Sequence【栈优化】
原文地址:http://blog.csdn.net/u013912596/article/details/38848331