http://acm.nyist.net/JudgeOnline/problem.php?pid=2
#include <stdio.h>
#include <string.h>
#define MAXN 10000
char astack[MAXN+2];
int main () {
int N;
scanf ("%d" , &N);
getchar();
while (N--) {
memset (astack , 0 , sizeof (astack));
char chr;
int i;
astack[1] = getchar();
for (i = 1; (chr = getchar()) != '\n';) {
if ((astack[i] == '(' && chr == ')') || (astack[i] == '[' && chr == ']')) {astack[i] = 0; i--;}
else {i++; astack[i] = chr;}
//此处不能用break提前跳出循环,会出现意想不到的错误
}
if (astack[1] == 0) printf ("Yes\n");
else printf ("No\n");
}
return 0;
}
用数组模拟栈,题目要求有最大空间,干脆直接用数组开辟好最大空间。