Check the legality of the stack-in stack

Source: Internet
Author: User

Stack and stack out stack

stack: is a linear table that restricts insertion or deletion at the end of the stack, also known as LIFO ( LIFO linear table, this feature can be expressed as the image of ... (railway dispatching station)

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/F6/wKioL1cOL87iQJbMAABWhZxsiWM922.jpg "title=" Picture 1.jpg "alt=" Wkiol1col87iqjbmaabwhzxsiwm922.jpg "/>

< /span> The same stack order can have different stacking order , the following is a partial out-of-stack order  

34521 25431 14532) 32145 43215

So how exactly does it verify that a stack sequence matches a stack sequence?

Idea : the stack and the stack sequence exist in the array, and then create a secondary stack, the the elements in the input sequence are pressed into the stack sequentially, and then popped out in sequence.

The stack and the stack sequence exist in two arrays, and then create a secondary stack, the elements in the input sequence are pressed into the stack, followed by the stack sequence pop-up.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/F9/wKiom1cOMxCCC8sJAAAwocnmuyc167.jpg "title=" Picture 2.jpg "alt=" wkiom1comxccc8sjaaawocnmuyc167.jpg "/>650) this.width=650; src=" Http://s5.51cto.com/wyfs02/M00/ 7e/f9/wkiom1com4nciioqaaaychby4ay149.jpg "title=" image 3.jpg "alt=" Wkiom1com4nciioqaaaychby4ay149.jpg "/>

Method: With pop-up sequence4,5,3,2,1For example, the first number that you want to be ejected is4, so you need to4is pressed into the stack, and the sequence pressed into the stack is determined by the input stack sequence, which is4guaranteed before entering the stack theIt's all in the stack. This time the stack contains4a number, respectively, is1,2,3,4, where4at the top of the stack, which corresponds to the first of the Stack array, pops up the top element of the stack4, the next element you want to eject is5, while5not in the stack, so you need to put the stack sequence4after the elements are pressed in, this time5at the top of the stack, you can bounce it out, and the next thing you want to be popped is3,2,1, they are located at the top of the stack before each operation, so it pops up directly.

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7E/F6/wKioL1cONLWBZn90AADayipLEfw837.jpg "title=" 3.jpg " alt= "Wkiol1conlwbzn90aadayiplefw837.jpg"/>

Code:

#include <iostream> #include <stack>using namespace std;bool check_push_pop (int*  Ppush,int* ppop,int length) {    if (length<=0 | |   ppush == null | |  ppop == null)     {        return  false;    }    int in = 0;     int out = 0;    stack<int> s;    // S.push (Ppush[0]);     int index = 0;  //number of elements pressed into the element      for (out = 0;out < length; out++)                 //(1) For loop control what     {         for (in = index;in <=length; in++)   //pPush[ 1,2,3,4,5]   ppop[4,5,3,2,1]                                                   {              if ((S.empty ()) | | S.top ()!=ppop[out])   //(2) Why do I have to empty               {                  if (in == length)                   //(3) If what is detected                   {                                             return false;                  }                  s.push (Ppush[in]);                  ++index;              }              else             {                 s.pop ();                 break;                         //jumps out of this layer for loop, making it traverse the next out stack element              }         }    }    if (S.empty ()  && in  == length && out==length)         return  True;    else        return false;} Void test1 () {    int push[5] = {1,2,3,4,5};    int  pop[5] = {4,5,3,2,1};    if (Check_push_pop (Push,Pop,5))      {        cout<< "input and output matching" <<endl;     }    else    {         cout<< "input and output mismatch" <<Endl;    }}void test2 () {    int push[5] = { 1,2,3,4,5};    int pop[5] = {4,5,3,1,2};    if (Check_ Push_pop (push,pop,5))     {        cout<< " Input and output matching "<<endl;    }    else    {         cout<< "input and output mismatch" <<endl;    }}int  main () {    test1 ();     //test2 ();     system ( "Pause");     return 0;}

Summarize:

If the next pop-up number is just the top number of the Stack, it pops up directly. If the next pop-up number is not on the top of the stack, we will press the number that is not in the stack in the stack into the secondary stack until the next number that needs to eject is pressed into the stack. If all the numbers are pressed into the stack and still find the next pop-up number, then the sequence cannot be a pop-up sequence.


for the stack sequence :1 2...I...J...K...N This is a subset of sequences. That is, if the stack sequence is abc , the stack sequence cannot be cab

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/F9/wKiom1cONQvyGE5kAAA3shzryKA109.jpg "title=" Picture 4.jpg "alt=" Wkiom1conqvyge5kaaa3shzryka109.jpg "/>


Examples:

the queued sequence for a stack is a,b,c,d,e , the possible stack sequence is ()

A. Adbec (DBC) b. Ebcad (EBC)

C. Bcdea D. EABCD(EAB)

The above laws can be used to draw C








Check the legality of the stack-in 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.