问题内容
Description
给定一串由左小括号,即“(”,和右小括号“)”组成的串,判断其是否匹配。
判断其合法的标准为即为数学等式中括号匹配的标准。
Input Format
第1行:N, 0<N≤500<N≤50;
第2至N + 1行,一个括号串,保证串的长度不超过100。
Output Format
共N行,若第i个串匹配,为“YES”,否则为“NO”。
Sample Input
3
(())
(()
)(
Sample Output
YES
NO
NO
代码实现
#include <stdio.h>
int main()
{
int n,cnt;
int flag = 1;
char in;
scanf("%d",&n);
getchar();
while(n--){
flag = 1;
cnt = 0;
while(1){
in = getchar();
if('(' == in){
cnt++;
}
else if(')' == in){
cnt--;
}
else {
break;
}
if(cnt < 0){
flag = 0;
}
}
if(!cnt && flag){
puts("YES");
}
else{
puts("NO");
}
}
return 0;
}
时间:0ms 空间:8812kb