Test instructions: To find the longest legal bracket sequence
Solution: Stack + Category Discussion
Now indicates that the sequence is already worked out, and this sequence may be able to connect with the sequence that is now scanned, and TMP indicates the sequence length
Left bracket in stack
Closing parenthesis: 1. Stack empty: Statistics current total length and stack, now,tmp empty
2. Stack is not empty: (1) match: tmp+2, the Stack, if the stack after the stack is empty, the now=now+tmp equivalent to the current calculated and the previous work together, because at this time the stack empty, there is no parenthesis block between the two sequences
(2) Mismatch: now=tmp=0, Stack empty
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <cstring>6#include <cstdlib>7#include <queue>8#include <vector>9#include <map>Ten#include <stack> One#include <string> A - using namespacestd; - the intT; - Chars[1000007]; - Charstk[1000007]; - + BOOLCheckCharC1,CharC2) { - if(c1=='('&& c2==')')return true; + if(c1=='{'&& c2=='}')return true; A if(c1=='['&& c2==']')return true; at return false; - } - - intMain () { -scanf"%d",&T); - for(intcas=1; cas<=t;cas++){ inscanf"%s", s); - intlen=strlen (s); to intans=0; + intnow=0; - inttmp=0; the inttop=0; * for(intI=0; i<len;i++){ $ if(s[i]==')'|| s[i]=='}'|| s[i]==']'){Panax Notoginseng if(top>0&&check (Stk[top],s[i])) { -tmp+=2; thetop--; + if(top==0){ Anow+=tmp; thetmp=0; +ans=Max (ans,now); - } $ } $ Else{ - if(top>0&&!check (Stk[top],s[i])) { -ans=Max (ans,tmp); thetop=0; -now=0;Wuyitmp=0; the } - Else{ Wu if(top==0){ -now=now+tmp; AboutAns=max (now+Tmp,ans); $now=tmp=0; - } - } - } A } + Else{ thetop++; -stk[top]=S[i]; $ } the } thecn1=Max (ans,tmp); the if(top==0){ theAns=max (ans,now+tmp); - } in if(ans==0) theprintf"Case #%d:i think H is wrong!\n", CAs); the Else Aboutprintf"Case #%d:%d\n", Cas,ans); the } the return 0; the } + /* - 3 the (){[]}Bayi {([(])} the ))[{}]] the - 8 - [()(()]{}()) the the 8 the [()(()()) the ([)(()()) - ()[(()()) the ()([()()) the ()(([)()) the ()(()]())94 ()(()()]) the ()(()())] the */
UESTC in Galgame We Trust