【CH1801】括號畫家
阿新 • • 發佈:2019-03-31
color div ret tdi continue sed space 更新 can
一道簡單的括號匹配問題,如果是左括號就入棧,如果是右括號且與棧頂匹配則答案加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];AC Code11 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]=‘\0‘; 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 }
【CH1801】括號畫家