用栈轻松解决: { Author:wzx961008 Problem:UVa 673-Parentheses Balance Verdict:Accepted Language:PASCAL Run Time:0.136s Submission Date:2011-02-07 06:40:45 } var n,i,top:longint; stack:array[1..128]of char; c:char; function ok(a,b:char):boolean; begin if (a='(')and(b=')') then exit(true); if (a='[')and(b=']') then exit(true); exit(false); end; begin readln(n); for i:=1 to n do begin top:=0; while not eoln do begin read(c); inc(top); stack[top]:=c; if (top>1)and(ok(stack[top-1],stack[top])) then top:=top-2; end; if top=0 then writeln('Yes') else writeln('No'); readln end; end.