uestc 10 In Galgame We Trust

题意:求最长的合法括号序列

解:栈+分类讨论

now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度

左括号入栈

右括号:1.栈空时:统计当前总长 并且将栈,now,tmp清空

2.栈不空:(1)匹配:tmp+2,弹栈,如果弹栈后栈为空,now=now+tmp相当于把现在算出的和之前算出的连起来,因为此时栈空,已经没有括号挡在两段序列之间

(2)不匹配:now=tmp=0,栈清空

 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>
10 #include<stack>
11 #include<string>
12 
13 using namespace std;
14 
15 int T;
16 char s[1000007];
17 char stk[1000007];
18 
19 bool check(char c1,char c2){
20     if (c1=='(' && c2==')') return true;
21     if (c1=='{' && c2=='}') return true;
22     if (c1=='[' && c2==']') return true;
23     return false;
24 }
25 
26 int main(){
27     scanf("%d",&T);
28     for (int cas=1;cas<=T;cas++){
29             scanf("%s",s);
30             int len=strlen(s);
31             int ans=0;
32             int now=0;
33             int tmp=0;
34             int top=0;
35             for (int i=0;i<len;i++){
36                     if (s[i]==')' || s[i]=='}' || s[i]==']'){
37                             if (top>0 && check(stk[top],s[i])){
38                                     tmp+=2;
39                                     top--;
40                                     if (top==0){
41                                             now+=tmp;
42                                             tmp=0;
43                                             ans=max(ans,now);
44                                     }
45                             }
46                             else{
47                                     if (top>0 && !check(stk[top],s[i])){
48                                             ans=max(ans,tmp);
49                                             top=0;
50                                             now=0;
51                                             tmp=0;
52                                     }
53                                     else{
54                                             if (top==0){
55                                                     now=now+tmp;
56                                                     ans=max(now+tmp,ans);
57                                                     now=tmp=0;
58                                             }
59                                     }
60                             }
61                     }
62                     else{
63                             top++;
64                             stk[top]=s[i];
65                     }
66             }
67             ans=max(ans,tmp);
68             if (top==0){
69                     ans=max(ans,now+tmp);
70             }
71             if (ans==0)
72                 printf("Case #%d: I think H is wrong!
",cas);
73             else
74                 printf("Case #%d: %d
",cas,ans);
75     }
76     return 0;
77 }
78 /*
79 3
80 (){[]}
81 {([(])}
82 ))[{}]]
83 
84 8
85 [()(()]{}())
86 
87 8
88 [()(()())
89 ([)(()())
90 ()[(()())
91 ()([()())
92 ()(([)())
93 ()(()]())
94 ()(()()])
95 ()(()())]
96 */
原文地址:https://www.cnblogs.com/baby-mouse/p/4445776.html