Stack-out stack sequence correctness analysis

Source: Internet
Author: User

The problem is simple: in the nature of the stack, the numbers of the 1~n are sequentially stacked, giving their stack sequence to determine whether the sequence is reasonable.

The complexity of this problem is considerably reduced compared to the previously encountered "solving all feasible stack sequences". However, in order to correctly simulate this process through code, in the logical processing also need to pay attention to a lot of details.

Here's how to deal with it first:

Program input: N as well as a given stack sequence, the stack sequence may be set to q[n+1], where q[1]~q[n] order to save the correctness of the unknown stack sequence.

Since the stack is in sequential order, it can be saved with no extra array, only one increment of flag A is required. Also, the stack sequence requires a pointer B to hold the current sequence header q[b].

Also requires a stack STK to hold the number of the stack

The loop performs the following steps:

(1) Compare the current sequence first q[b] and a is equal, equal to the current into the stack number A, it immediately after the stack, a self-increment 1, the stack sequence pointer back, that is, B self-increment 1

(2) if not equal, then to determine whether the top element of the stack is equal to the current sequence header, if equal, the stack top element out of the stack, the stack sequence pointer continues to move.

(3) if (1) (2) is not satisfied, then determine whether a is already greater than N, greater than not meet the stack condition, and jump out of the loop, less than equals will be the current number a stack, and self-increment 1, jump (1)

C + + Implementation code is as follows

1#include <iostream>2#include <stack>3 using namespacestd;4 intN//Number of inputs5 intf[ -];//Save the team sequence6stack<int>Stk;7 intMain () {8Cin>>N;9      for(intI=1; i<=n;++i)TenCin>>F[i]; One     intb=1, a=1;//out of the team sequence pointer A     BOOLflag=1; -      while(b<=N) { -         if(f[b]==a) {//Direct comparison of the header of the stack sequence with the current sequence header thea++; -b++; -         } -         Else if(!stk.empty () && stk.top () ==f[b]) {//Compare stack tops and headers + Stk.pop (); -b++; +         }  A         Else if(a<=n) {//If a has any remaining elements, atStk.push (a++);  -         } -         Else{ -flag=0; Break; -         }  -     }  in     if(flag) cout<<"Yes"; -     Elsecout<<"No"; to     return 0; +}

Stack-out stack sequence correctness analysis

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.