'commit'
This commit is contained in:
93
C++课程/待整理知识/啊哈算法/p35.cpp
Normal file
93
C++课程/待整理知识/啊哈算法/p35.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#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;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user