(あとで)
↓実装練習 (accepted in practice)
int cum[100010]; // i is # of '[' in S[k] for k in [0, i) int count(int L, int R) { return cum[R+1]-cum[L]; } int main() { //ios::sync_with_stdio(false); string S; while(cin>>S) { cum[0] = 0; REP(i, S.size()) { cum[i+1] = cum[i] + (S[i]=='['); } deque<pair<char, int> > mo; int N=S.size(); int L=-1, R=-1; int co = 0; int start = 0; REP(i, N) { char op = S[i]==']' ? '[' : '('; if(S[i]=='[' || S[i]=='(') { mo.PB(MP(S[i], i)); } else { if(mo.size() && mo.back().first==op) { mo.pop_back(); int NL = start; if(mo.size()) NL = mo.back().second + 1; int NR = i; int nco = count(NL, NR); if( co < nco ) { co = nco; L=NL; R=NR; } } else { mo.clear(); start = i+1; } } } cout<<co<<endl; if(co>0) cout<<S.substr(L, R-L+1)<<endl; } return 0; }