【CH1801】括号画家

一道简单的括号匹配问题,如果是左括号就入栈,如果是右括号且与栈顶匹配则答案加2,如果不匹配则答案清零,每次都更新一下最优答案即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char s[100005];
 6 char stk[100005];
 7 int top,maxlen=0;
 8 int main() {
 9     scanf("%s",s);
10     stk[++top]=s[0];
11     int len=0;
12     for (int i=1; i<strlen(s); i++) {
13         if (s[i]==stk[top]+1 || s[i]==stk[top]+2) {
14             len+=2;
15             stk[top]='';
16             top--;
17             maxlen=max(maxlen,len);
18             continue;
19         }
20         if (s[i]==40 || s[i]==91 || s[i]==123) stk[++top]=s[i];
21         else {
22             top=len=0;
23             memset(stk,0,sizeof(stk));
24         }
25     }
26     printf("%d",maxlen);
27     return 0;
28 }
AC Code
原文地址:https://www.cnblogs.com/shl-blog/p/10631969.html