[Programming question] stack push and pop Sequence

Source: Internet
Author: User

29. Stack push and pop sequence (stack)
Question: enter two integer sequences. One sequence represents the push order of the stack,
Determine whether another 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, if the input push sequence is 1, 2, 3, 4, 5, 3, 2, and 1, it may be a pop series.
Because there can be the following push and pop sequences:
Push 1, Push 2, Push 3, Push 4, Pop, Push 5, pop,
The pop sequence is 4, 5, 3, 2, and 1.
However, sequences 4, 3, 5, 1, and 2 cannot be pop sequences of push sequences 1, 2, 3, 4, and 5.

 

Ideas:

First, I recorded the position of each element in each POP sequence in the push sequence. This is like forcing the push sequence to 0 1 2 3 4..., so the push sequence is arranged from small to large.

Then, observe the push sequence arranged from small to large, and the characteristics of its pop sequence.

4 5 3 2 1

Record 1st numbers and find the increasing number. Here is 4 5. They are incrementing.

3 4 2 1 5

3 4 5 incremental

It is found that as long as these numbers conform to the increasing law, the pop sequence is reasonable. These sequences are the values that start to pop up after a new number is added.

Take 3 4 2 1 5 for example.

3 is the first value popped up after pressing 1 2 3.

4 is the first value popped up after Press 4.

5 is the first value popped up after pressing 5.

The descending sequence of 4 2 1 indicates that a new value is not pushed into the sequence.

That is, an incremental sequence indicates that a new value is pushed in, and a descending sequence indicates that a new value is always displayed. Because the push sequence increases progressively, the new value can only increase.

 

For 4, 3, 5, 1, 2

4 5 2 obviously does not satisfy the increasing relationship. That is, after being pushed to 5, it cannot be pushed to 2. It is not a legal pop sequence.

 

Note: I cannot prove it. I don't know if all of them comply with this rule. Who can give an example?

/* 29. Stack push and pop sequence (stack) Question: 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, if the input push sequence is 1, 2, 3, 4, 5, 3, 2, and 1, it may be a pop series. It can have the following push and pop sequences: Push 1, Push 2, Push 3, Push 4, Pop, Push 5, pop, the pop sequence is 4, 5, 3, 2, and 1. However, sequences 4, 3, 5, 1, and 2 cannot be pop sequences of push sequences 1, 2, 3, 4, and 5. Start time = 16: 15end time = 17:20 */# include <stdio. h> int * getlocation (INT data, int * poplist, int Len) {for (INT I = 0; I <Len; I ++) {If (Data = poplist [I]) return poplist + I;} return NULL;} bool ispop (int * pushlist, int * poplist, int Len) {for (INT I = 0; I <Len; I ++) // record the position where the number in pop corresponds to {int * loc = getlocation (pushlist [I], poplist, Len); If (loc! = NULL) {* loc = I;} else {printf ("error: the two list have different member! ") ;}} Int increasenum2 = poplist [0]; int increasenum1; For (INT I = 1; I <Len; I ++) {If (poplist [I]> poplist [I-1]) {increasenum1 = increasenum2; increasenum2 = poplist [I]; If (increasenum2 <increasenum1) return false ;}} return true;} int main () {int A [5] = {1, 2, 3, 4, 5}; int B [5] = {5, 3, 4, 2, 1 }; bool is = ispop (A, B, 5); Return 0 ;}

 

Online answer: the ideas on the Internet are surprisingly consistent. They all directly build a stack to simulate this process. It is better than my thinking, less computing, and better understanding.

Http://blog.sina.com.cn/s/blog_a46817ff01019vtd.html

If we want the pop number to be the top number of the stack, we can directly pop out of the stack; if we want the pop number to be not at the top of the stack, we will not go to the push Sequence
* Search for this number from the number pushed to the stack, and push all the numbers before it into the stack. If all the numbers are pushed to the stack, this is still not found.
* Digits, indicating that the sequence cannot be a pop sequence.

Online casually found a code, not verified http://www.cnblogs.com/aLittleBitCool/archive/2011/03/05/1971689.html

// Stack push and pop sequence # include <iostream> # include <stack> const int size = 5; // defines the length of using namespace STD; bool judge (INT spush [], int spop []) {stack <int> my_stack; int ipush = 0, ipop = 0; while (ipush <size) {// when the stack top and pop are equal, compare the top of the stack after pop with the elements after pop until the cout <"push" <spush [ipush] <Endl; // test my_stack.push (spush [ipush]); While (! My_stack.empty () & ipop! = 5 & my_stack.top () = spop [ipop]) {// careful array out-of-bounds cout <"pop" <spop [ipop] <Endl; // test ipop ++; my_stack.pop ();} ipush ++;} If (ipop = size) return true; else return false;} int main (void) {int spush [size], spop [size]; for (INT I = 0; I <size; I ++) CIN> spush [I]; for (INT I = 0; I <size; I ++) CIN> spop [I]; If (Judge (spush, spop )) cout <"yes" <Endl; else cout <"no" <Endl; System ("pause"); Return 0 ;}

 

[Programming question] stack push and pop Sequence

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.