Title: Enter a sequence of two integers, the first sequence represents the stacking order of the stack, and determine if the second sequence is the pop-up order for the stack. Assume that all the numbers that are pressed into the stack are not equal. For example, sequence 1, 2, 3, 4, 5 is a stack of the stacking sequence, sequence 4,5,3,2,1 is the stack sequence corresponding to a pop-up sequence, but 4,3,5,1,2 is not likely to be the stack sequence of pop-up sequence. How to solve the problem: if the next pop-up number is just the top number of the stack, then pop directly. If the next pop-up number is not on the top of the stack, we will press the number that is not yet in the stack in the stack sequence into the auxiliary stack until the next number that needs to eject is pressed into the top of the stack. If all the numbers are pressed into the stack and the next pop-up number is not found, then the sequence cannot be a pop-up sequence.
1 BOOLIspoporder (Const int* Ppush,Const int* Ppop,intnlength)2 {3 BOOLbpossible=false;4 if(ppush!=null&&ppop!=null&&nlength>0)5 {6 Const int* pnextpush=Ppush;7 Const int* pnextpop=Ppop;8std::stack<int>Stackdata;9 Ten while(pnextpop-ppop<nlength) One { A while(Stackdata.empty () | | Stackdata.top ()!=*Pnextpop) - { - if(pnextpush-ppush==nlength) the Break; -Stackdata.push (*Pnextpush); -pnextpush++; - } + if(Stackdata.top ()!=*Pnextpop) - Break; + A Stackdata.pop (); atpnextpop++; - } - if(Stackdata.empty () &&pnextpop-ppop==nlength) -bpossible=true; - } - returnbpossible; in}
Stack push-in, pop-up sequence