Topic source "Data structure and algorithm surface test 80". Here gives my solution, if you have a better solution, welcome message.
Problem Analysis: The basic operation of the test stack, the stack is a "advanced after-out" data structure. Determining whether a sequence is a stack of pop sequences is a common problem that can be simulated by the push and pop processes, push and pop are always paired, such as:
Method:
#define PUSH 1 #define POP-1 bool Judge_push_pop (int *a, int *b, int len_a, int len_b) {if (NULL = = A | | NULL = = B | |
Len_a! = Len_b) return false;
int *p_a = A;
int *p_b = b;
int *OP = (int *) malloc (sizeof (int) * LEN_A * 2);
int index = 0;
int i = 0;
while (I < len_a) {Op[index] = push;
Index + +;
if (*p_a = = *p_b) {p_b + +;
Op[index] = Pop;
Index + +;
} p_a + +;
i + +;
} while (Index < LEN_A * 2) {op[index++] = Pop;
}//Judge int start = 0;
int end = Len_a * 2-1;
while (Start < end) {if (Op[start] + op[end] = = 0) {start + +;
End--;
}else return false;
} return true;
Free (OP); }