Stack push-in

Source: Internet
Author: User

Enter Two integer sequences. the first sequence indicates the order in which the stack is pushed. Determine whether the second sequence is in the pop-up order of the stack. Assume that all numbers pushed into the stack are not equal. For example, the sequence 1, 2, 3, 4, and 5 is the order in which a stack is pushed. The sequence 4, 5, 3, 2, and 1 is a pop-up sequence corresponding to the stack sequence, but the sequence 4, 3, 5, 2. It cannot be the pop-up sequence of the Pressure stack sequence. Input: each test case contains three rows: the first row is an integer n (1 <= n <= 100000), indicating the length of the sequence. The second row contains n integers, indicating the order of stack pushing. The third row contains n integers, indicating the pop-up sequence of the stack. Output: corresponds to each test case. If the second sequence is the pop-up sequence of the first sequence, output Yes; otherwise, output No. Sample input: 51 2 3 4 54 5 3 2 151 2 3 4 54 3 5 1 2 sample output: YesNo code AC: idea: scan + inbound stack + Stack: the current output is equal to the top element of the stack. If the current output is not the top element of the stack, it is necessary to import the element into the stack until this element is generated. If this element is not found in the stack, this is the sequence of errors. [Cpp] # include <stdio. h> # include <stdlib. h> int main () {int n, * stack1, * stack2, * stack, I, j, top =-1, flag, f; while (scanf ("% d", & n )! = EOF) {stack1 = (int *) malloc (sizeof (int) * n); stack2 = (int *) malloc (sizeof (int) * n ); stack = (int *) malloc (sizeof (int) * n); for (I = 0; I <n; I ++) {scanf ("% d ", & stack1 [I]) ;}for (I = 0; I <n; I ++) {scanf ("% d", & stack2 [I]);} top =-1; I = 0; j = 0; flag = 1; while (stack1 [I]! = Stack2 [j]) {stack [++ top] = stack1 [I]; if (++ I) >=n) {printf ("No \ n "); flag = 0; break;} if (! Flag) {continue;} I ++; j ++; while (j <n) {f = 0; if (top>-1) {if (stack2 [j] = stack [top]) {top --; j ++;} else {f = 1 ;}} else {f = 1 ;} if (f) {while (stack2 [j]! = Stack1 [I]) {stack [++ top] = stack1 [I]; I ++; if (I> = n) {printf ("No \ n "); flag = 0; break; }}if (flag) {stack [++ top] = stack1 [I] ;}} if (! Flag) {break ;}} if (! Flag) {continue;} printf ("Yes \ n");} return 0 ;}

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.