-
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