Nine-degree 1366-stack push-in pop-up sequence

Source: Internet
Author: User

Title Description:

Enter a sequence of two integers, and the first sequence represents the stacking order of the stack, judging whether the second sequence is the pop-up order for the stack. Assume that all the numbers that are pressed into the stack are not equal. For example, the sequence 1,2,3,4,5 is the indentation order of a stack, and the sequence 4,5,3,2,1 is a pop-up sequence corresponding to the stack sequence, but 4,3,5,1,2 is not likely to be the pop-up sequence of the stack sequence.

Input:

Each test case consists of 3 rows:

The first behavior is 1 integer n (1<=n<=100000), which represents the length of the sequence.

The second row contains n integers that represent the stacking order of the stack.

The third row contains n integers that represent the pop-up order of the stack.

Output:

corresponding to each test case, if the second sequence is the first sequence of the pop-up sequence output yes, otherwise output No.

Sample input:
5
1 2 3) 4 5
4 5 3) 2 1
5
1 2 3) 4 5
4 3 5) 1 2

Sample output:

Yes

No

#include <cstdlib> #include <cstdio> #include <stack> #include <vector> using namespace std; BOOL Ispoporder (const vector<int> &push, const vector<int> &pop, int length) {bool sequence = Fals           E           if (Push.empty () | | pop.empty () | | length < 0) return sequence;     Vector<int>::const_iterator Inextpush = Push.begin ();           Vector<int>::const_iterator Inextpop = Pop.begin ();           Stack<int> Stack;              while (Inextpop-pop.begin () < length) {while (Stack.empty () | | stack.top ()! = *inextpop) {                             if (inextpush-push.begin () = = length) break;              Stack.push (*inextpush);              ++inextpush;                   } if (Stack.top ()! = *inextpop) break;         Stack.pop ();     ++inextpop;       } if (Stack.empty () && inextpop-pop.begin () = = length)//This is if not while   sequence = true; return sequence;}    int main (int argc, char *argv[]) {int n;        while (scanf ("%d", &n)! = EOF) {int value;                 Vector<int> push, Pop;        int i;            for (i = 0; i < n; ++i) {scanf ("%d", &value);        Push.push_back (value);            } for (i = 0; i < n; ++i) {scanf ("%d", &value);        Pop.push_back (value);        } if (Ispoporder (push, Pop, N)) printf ("yes\n");                      else printf ("no\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nine-degree 1366-stack push-in pop-up 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.