Determine if the stack order is legitimate given the stacking order

Source: Internet
Author: User

Title Description: Given a stack sequence, given a stack sequence, determine whether the stack sequence is legitimate.

Analysis: If the stack sequence is 1 2 3 4 5, determine whether 4 5 3 2 1 is a valid stack order.

Two sequences are given in the form of an array

Starting from the first element of the two array, if the stack is empty , or if the top element of the stack is not equal to the element corresponding to the current subscript array , the element pointed to by the current in-stack array is stacked

The initial state is as follows:

650) this.width=650; "src=" "title=" 1.jpg " alt= "Wkiol1cdyrli8xhmaaazr1lwyse538.jpg"/>

Step 1: Push the 1 to the stack and move the subscript back, as shown in

650) this.width=650; "src=" "title=" 2.jpg " alt= "Wkiom1cdylkbp0uvaaazxjdskec733.jpg"/>

Step 2: To determine and press the stack sequentially, when the 4 stack, the top of the stack is equal to the stack array subscript point to the element, will be 4 out of the stack, as shown in

650) this.width=650; "src=" "title=" 3.jpg " alt= "Wkiom1cdyyqjn0kraaacrb47etm216.jpg"/>

Step 3: The 4 out of the stack, and the stack array after the subscript move, continue to determine whether the top element of the stack is the current stack array subscript corresponding element, yes, then out of the stack, or continue to execute backwards

650) this.width=650; "src=" "title=" 4.jpg " alt= "Wkiol1cdzf3am8avaaagxkz3qv0572.jpg"/>

Step 4: Repeat the above steps until the subscript in the array into the stack goes to the end 650) this.width=650; "Src=" Wkiol1cdzpqqws4maaaifxitk1q465.jpg "title=" 5.jpg "alt=" Wkiol1cdzpqqws4maaaifxitk1q465.jpg "/>

When the last element into the stack, at this time simply loop to determine whether the top element of the stack is the same as the current index of the stack array is equal to the element, if the sequence is valid, the stack of elements will eventually be out of the stack, the rule stack is never empty

650) this.width=650; "src=" "title=" 6.jpg " alt= "Wkiom1cdzayridtnaaajyybab20771.jpg"/>

At this point, the stack is empty, then the sequence is valid.

The code is as follows:

#include <iostream> #include <stack> #include <cassert>using namespace std;template< Class t>bool issequencevaild (Const t* insequence, const t* outsequence,  size_t n) {assert (insequence && outsequence); stack<t> sc;int j  = 0;  //is used to record the subscript for  of the current element of the stack array (int i = 0; i < n;  ++i) {Sc.push (Insequence[i]);while  (!sc.empty ()  && ()  == outsequence[j]) {Sc.pop (); ++j;}} return  (Sc.size ()  == 0)  ? true : false;} Void test1 () {int in[5] = { 1, 2, 3, 4, 5 };int out1[5]  = { 1, 2, 3, 4, 5 };int out2[5] = { 4, 5,  3, 2, 1 };int out3[5] = { 3, 5, 4, 2, 1 }; Int out4[5] = { 3,&nBsp;5, 4, 2, 1 };int out[5] = { 4, 5, 3, 1, 2  };cout << IsSequenceVaild<int> (in, out1, 5)  << endl;cout  << IsSequenceVaild<int> (in, out2, 5)  << endl;cout < < IsSequenceVaild<int> (in, out3, 5)  << endl;} Void main () {Test1 (); System ("Pause");}

This article is from the "11408774" blog, please be sure to keep this source

Determine if the stack order is legitimate given the stacking order

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: 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.