Files
python/C++课程/待整理知识/啊哈算法/p35.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

94 lines
1.5 KiB
C++

#include<bits/stdc++.h>
// Õ»
struct Stack {
int s[100];
int top;
} stack;
int main(void) {
char left_d = '{';
char left_z = '[';
char left_x = '(';
char right_d = '}';
char right_z = ']';
char right_x = ')';
int flag = 1;
int have_other_word = 0;
int i;
// ¶ÓÁÐ
char que[100];
int head = 0;
int tail = 0;
stack.top = 0;
gets(que);
tail = strlen(que);
for (i = 0; i < tail; i++) {
if (
que[i] != left_d &&
que[i] != left_z &&
que[i] != left_x &&
que[i] != right_d &&
que[i] != right_z &&
que[i] != right_x
) {
have_other_word = 1;
break;
}
}
if (have_other_word == 0) {
while (head < tail) {
if (que[head] == left_d || que[head] == left_z || que[head] == left_x) {
stack.s[stack.top] = head;
stack.top++;
} else {
if (que[head] == right_d) {
if (que[stack.s[stack.top - 1]] == left_d) {
stack.top--;
} else {
flag = 0;
}
}
if (que[head] == right_z) {
if (que[stack.s[stack.top - 1]] == left_z) {
stack.top--;
} else {
flag = 0;
}
}
if (que[head] == right_x) {
if (que[stack.s[stack.top - 1]] == left_x) {
stack.top--;
} else {
flag = 0;
}
}
}
if (flag == 0) {
break;
}
head++;
}
} else {
flag = 0;
}
if (flag) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}