Talk about how to determine whether a pop sequence is a pop sequence of a push series

Source: Internet
Author: User
Tags stack pop

 

have such a topic:

Enter a sequence of two integers. One of these sequences represents the push order of the stack,

Determine if another sequence is possible for the corresponding pop sequence.

 

let's simulate the process of the stack and the stack:

Here are three roles:

Push sequence: It has only one action that is to constantly push its own elements into stack stack, until the end;

Stack stack: It has two actions, one is to push the incoming elements in, one is the existing elements pop out, push elements from the push sequence, pop elements from the pop sequence output;

Pop sequence: It has only one action, that is, constantly receive from the stack stack pops out of the elements and print out;

Assuming that at some point the push sequence just pushes element a into stack stack, the pop sequence just outputs the element b of the stack pop, andB has three different output:

1. Stack pops up an element;

2. A press in and eject immediately;

3. A press in, then press into a number of elements in the S2, and then eject an element;

If the above conditions are not satisfied, the pop sequence is not the sequence of pop corresponding to the push sequence.

The possible conditions of B at this time are:

1. b is the element C at the top of the stack Stack, which pops up the stack top element;

2. B is element A, this situation is a just being pushed in, is pop out, so B equals A;

3. If it is not 1 and 2, that B can only be S2 elements, therefore, you should put a pressure into the stack, and then continue to the next, and then take out the elements in S2 to do the above comparison;

4. Step 2, 3 needs to satisfy the push sequence has not yet ended, if 1, 2, 3 are not satisfied, that this pop sequence is not the corresponding pop sequence of the push series.

So we get this piece of code:

#include <iostream> #include <stack> using namespace std;
	BOOL Ispopsequence (const int pusharray[], const int poparray[], int _size) {const int *ppush=pusharray;
	const int *ppop=poparray;
	Stack<int> Stdata;
	int cnt=0;
		while (Ppop-poparray < _size) {if (Ppush-pusharray) >= _size && stdata.empty ()) break;  If the push \ still not empty, then push the element into the queue, and continue if (!stdata.empty () &&
			Stdata.top () = = *ppop) {stdata.pop ();
		ppop++; //if current element is equal to the element in pop queue, both move forward else if (Ppush-pusharray) < _size
			&& *ppush = = *ppop) {ppush++;
		ppop++; }//if the top element of the "stack is" equal to current element, the pop it, POPs queue move forward else if (ppush-p
			Usharray) < _size) {Stdata.push (*ppush);
		ppush++;
	}//else the sequence is isn't matched else return false;
return true; int main () {int a[5]={1,2,3,4,5};
	int b[5]={2,5,4,3,1};
	if (Ispopsequence (a,b,5)) cout<< "True" <<endl;

	else cout<< "False" <<endl;
return 0;
 }

Test results are:

True

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.