22-Determines whether a sequence is another sequence of pop-up sequences into the stack

Source: Internet
Author: User

Title Description:
Enter a sequence of two integers, and the first sequence represents the stacking order of the stack, judging whether 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, the sequence 1,2,3,4,5 is the indentation order of a stack, and the sequence 4,5,3,2,1 is a pop-up sequence corresponding to the stack sequence, but 4,3,5,1,2 is not likely to be the pop-up sequence of the stack sequence.

Analytical:
Note: when it is impossible to solve the problem abstractly, we should give the positive and negative examples to analyze the problem and find out the law.

To determine if an input sequence is stacked in sequence, can a given output sequence be obtained?
Then we need a stack to put the input sequence into the stack in the order of the given output sequence.
Such as:
-Determine if 4,5,3,2,1 is 1,2,3,4,5 is the press-in order of a stack
The first one wants to pop up 4, then the 4 should be on top of the stack, and before it presses into 4, the three-step is already in the stack. 4 after the stack, want to pop 5, 5 not on the top of the stack, continue to press input until the top of the stack is 5, pop 5, want to pop 3, found 3 at the top of the stack, pop 3, want to pop 2, 1, found in the top of the stack. Then the sequence is a pop-up sequence.
-Determine if 4,3,5,1,2 is 1,2,3,4,5 is the press-in order of a stack
The first one wants to pop up 4, then the 4 should be on top of the stack, and before it presses into 4, the three-step is already in the stack. 4 out of the stack, want to pop 3, found 3 at the top of the stack, pop 3, want to eject 5, 5 is not on the top of the stack, continue to press input until the top of the stack is 5, pop 5, want to pop 1, found that 1 is not the top of the stack, continue to press input, found no input. Then the sequence is not a pop-up sequence.

Summary: Determine whether a sequence is a stack of pop-up sequence rule: If the next number you want to pop up is just the top of the Stack, it pops up directly; if the next number is not on the top of the stack, we will press the stack without the stack in the stack until the next number to eject is pressed into the top of the stack. If all the numbers are in the stack and the next number is not found, then the sequence is not a pop-up sequence.
< note: The above analysis comes from the point of the sword.

#include <iostream>#include <stack>using namespace STD;BOOLIspoporder (int* Ppush,int* Ppop,intLen) {BOOLIs_possible =false;if(Ppush = = NULL | | ppop = = NULL | | Len <=0)returnis_possible; Stack<int>Sinti =0, j =0;//I index Ppop, J index Ppush     while(I < Len) { while(S.empty () | | ppop[i]! = S.top ()) {//press stack until stack top element equals output queueS.push (ppush[j++]);if(j = len)//input has been pressed out                 Break; }if(Ppop[i] = = S.top ()) {//output sequence and stack top consistent, continuei++;        S.pop (); }Else{ Break;//After all input, still unequal, the result is: not the stack sequence}    }if(i = = Len && s.empty ()) is_possible =true;returnis_possible;}intMain () {int* input =New int[5];int* OUTPUT1 =New int[5];int* Output2 =New int[5];intA[] = {4,5,3,2,1};intB[] = {4,3,5,1,2}; for(inti =0; I <5; i++) {Input[i] = i+1;        Output1[i] = A[i];    Output2[i] = B[i]; }cout<< Ispoporder (input, OUTPUT1,5) << Endl;cout<< Ispoporder (input, Output2,5) << Endl;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

22-Determines whether a sequence is another sequence of pop-up sequences into the stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.