您的位置:首页 > 其它

UVA - 673 Parentheses Balance

2016-07-21 20:52 567 查看
题目大意:匹配括号。空行也输出 Yes

注意这种:([(])) ——> No

解题思路:左括号入栈,右括号出栈,最终栈空 Yes

一开始想用计数偷懒,但是发现太天真了,([(]))这种情况无法解决= =

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctype.h>
using namespace std;
char str[150];
char stack[150];
int main() {
int n;
scanf("%d", &n);
getchar();
while (n--) {
memset (stack, 0, sizeof(stack));
gets(str);
int tag = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == '(' || str[i] == '[') {
stack[tag] = str[i];
tag++;
}
if (str[i] == ')') {
if (stack[tag-1] == '(') {
stack[tag-1] = '\0';
tag--;
}
else {
stack[tag] = str[i];
tag++;
}
}
if (str[i] == ']') {
if (stack[tag-1] == '[') {
stack[tag-1] = '\0';
tag--;
}
else {
stack[tag] = str[i];
tag++;
}
}
}
if (stack[0] == '\0')
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva