Description: enter two integer sequences. One sequence represents the push sequence of the stack and determines whether the other sequence may be in the pop sequence. For the sake of simplicity, we assume that any two integers in the push sequence are not equal. For example, the input push sequence is 1, 2, 3, 4, 5, 6, and 7, SO 2, 1, 4, 3, 7, 6, 5 may be a pop series. However, sequences 4, 3, 5, 1, 2, 7, and 6 cannot be pop sequences of push sequences 1, 2, 3, 4, and 5.
Problem Analysis: To solve this problem, we can apply for a stack and then judge whether it is equal to the output sequence header one by one starting from the input sequence. Here is a simple example. For example, if the input sequence is 1, 2, 3, and 4, the output sequence is 3, 4, 2, and 1, which is the first number of the output sequence is 3, we will start searching for 3 from the input sequence, until 3 is found, and if there is data before 3, we store them in the stack. In the input sequence, we first encounter 1 element, which is not equal to the first element in the output sequence, we put 1 in the stack, and then there is 2 elements, and we don't want to wait. We also put it into the stack, and then there is 3. At this time, it is equal to the first element of the output sequence, we move the subscript of the output sequence to 2, and the subscript of the input sequence to 3. At this time, the element of the output sequence is 4. First, compare the elements at the top of the stack and find that they are not equal, at this time, the elements are either behind the input sequence, or there is no such element. We look for it in the input sequence. At this time, the input and output sequences indicate that element 4 is exactly the same as the elements in the output sequence, so we add 1 to both the output sequence and the lower mark of the input sequence. At this time, the input sequence has been completed, and the output sequence points to 2. We first compared them with the top elements of the stack and found they are equal, so we delete the top element of the stack, and add the subscript of the output sequence to 1. At this time, the output sequence is still to element 1. we compare it with the top element of the stack and find that they are equal, so we set Delete the top element of the stack and Add 1 to the subscript of the output sequence. At this time, we find that the stack is empty, and the subscript of the input sequence is till the end of the input sequence. This indicates that the output sequence is the output sequence of the stack, and true is returned; otherwise, false is returned; the code implementation is as follows:
# Include <iostream>/** author: w397090770 * Email: wyphao.2007@163.com * is only used for learning communication **/# include <vector> # include <stack> using namespace std; bool IsPopOrder (const vector <int> & Push, const vector <int> & Pop) {if (Push. size ()! = Pop. size () {return false;} stack <int> temp; int tempIndex = 0; int tempIndex2 = 0; int Size = Pop. size (); while (tempIndex2 <Size) {for (; tempIndex <Size; tempIndex ++) {if (Push [tempIndex] = Pop [tempIndex2]) {tempIndex2 ++; tempIndex ++; break;} temp. push (Push [tempIndex]);} if (! Temp. empty () & temp. top ()! = Pop [tempIndex2]) {for (; tempIndex <Size; tempIndex ++) {if (Push [tempIndex] = Pop [tempIndex2]) {tempIndex2 ++; tempIndex ++; break;} temp. push (Push [tempIndex]) ;}} else if (! Temp. empty () {temp. pop (); tempIndex2 ++;} if (! Temp. empty () & tempIndex> = Size & temp. top ()! = Pop [tempIndex2]) {return false;} else {while (! Temp. empty () & temp. top () = Pop [tempIndex2]) {temp. pop (); tempIndex2 ++ ;}} if (temp. empty () & tempIndex> = Size) {return true;} return false;} int main () {vector <int> Push, Pop; // 1 1 // 1 2 // 1, 2, 3, 4, 53, 5, 4, 1, 2 // 1, 2, 3, 4, 54, 3, 5, 1, 2 // 1, 2, 3, 4, 53, 5, 4, 2, 1 // 1, 2, 3, 4, 54, 5, 3, 2, 1 // 1, 2, 3, 4, 53, 5, 4, 2, 1Push. push_back (1); Push. push_back (2); Push. push_back (3); Push. push_back (4); Push. push_back (5); Push. push_back (6); Push. push_back (7); Pop. push_back (2); Pop. push_back (1); Pop. push_back (4); Pop. push_back (3); Pop. push_back (7); Pop. push_back (5); Pop. push_back (6); cout <IsPopOrder (Push, Pop) <endl; return 0 ;}