-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA673-parentheses-balance.c
85 lines (79 loc) · 1.83 KB
/
UVA673-parentheses-balance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdbool.h>
#define MAX_STACK 129
int stack[MAX_STACK];
int top = 0; // top = 1 时, 元素有一个, 元素位置在 stack[0],
// 弹出栈顶元素
void pop() {
if (top != 0) {
top -= 1;
}
else printf("The stack is empty"); // The stack is empty.
}
// 压栈
void push(int x) {
if (top >= MAX_STACK) {
printf("Stack overflow."); // Stack overflow.
}
stack[top] = x;
top ++;
}
// 查找栈顶元素
int find_top() {
if (top != 0) {
return stack[top - 1];
}
// ptintf("The stack is empty.");
return -1;
}
// 把右括号全部换成左括号
char turn_left(int i) {
if (i == ')') {
return '(';
}
if (i == ']') {
return '[';
}
return '{';
}
int main() {
int n, j;
char str[MAX_STACK];
scanf("%d\n", &n);
for(int i = 0; i < n; i++) {
top = 0; // 注意 top 要清空
scanf("%s", str); //https://stackoverflow.com/questions/5406935/reading-a-string-with-scanf
// printf("%s\n", str);
// continue;
int k = 0;
int flag = 0;
while ((j = str[k++]) != 0) {
if (j == '(' || j == '[' || j == '{') {
push(j);
}
if (j == ')' || j == ']' || j == '}') {
if (top == 0) {
printf("No\n");
flag = 1;
break;
}
if (turn_left(j) == find_top()) {
pop();
}
else {
printf("No\n");
flag = 1;
break;
}
}
}
if (flag == 1) {
continue;
}
if (top == 0) {
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}