括号匹配(栈做法)
1 #include <stdio.h> 2 #include <string.h> 3 4 char s[10002]; //栈 5 6 int main() 7 { 8 int n; 9 scanf("%d", &n); 10 getchar(); 11 memset(s, 0, sizeof(s)); 12 while (n--) 13 { 14 int top = 0;//栈标始终指向栈顶元素 15 char c; 16 while ((c = getchar()) != '\n' ) 17 { 18 if ((c == ')' && s[top] == '(') || (c == ']' && s[top] == '['))//如果匹配弹栈否则压栈 19 top--; 20 else 21 s[++top] = c; 22 } 23 24 if (!top) //栈为空匹配成功否则失败 25 printf("Yes\n"); 26 else 27 printf("No\n"); 28 29 } 30 return 0; 31 }
浙公网安备 33010602011771号