The sword refers to the offer: Stack's push-in popup sequence

Source: Internet
Author: User
Tags bool exit include printf

Sword refers to the 22nd question on offer, nine degrees OJ on AC.

Topic Description:

Enter a sequence of two integers, and the first sequence represents the stack's indentation order, and determine whether the second sequence is the pop-up order of the stack. Suppose all the numbers on the stack are not equal. For example, the sequence 1,2,3,4,5 is the pressing 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 cannot be the pop-up sequence of the pressure stack sequence.

Input:

Each test case consists of 3 lines:

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

The second row contains n integers that indicate the order in which the stack is pushed.

The third row contains n integers, which indicates the stack's pop-up order.

Output:

For each test case, output No if the second sequence is the pop-up sequence output yes for the first sequence.

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

The decision method is as follows:

If the element currently being judged in the second sequence is just the same as the top element of the stack, then pop out directly, if not unequal, then the first sequence is not yet into the stack of elements into the stack, until the element is equal to the stack, if all elements of the first sequence into the stack, there is no equivalent to find the element, Indicates that the second sequence is not a pop-up sequence of the first sequence,

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The AC code is as follows:

* * This program uses the array simulation stack */typedef int ELEMTYPE; #define MAX 100000//Stack depth #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int to  
p =-1; /* Press the data in stack A on top of the stack index pointer (int *a,elemtype data) {if (top>=max-1 | top<-1) retur  
      
    n false;  
    A[++top] = data;  
return true;  
      
    }/* The stack/bool Pop () {if (top<0) return False when the top index pointer is on the stack;  
    top--;  
return true; }/* To determine if Poplist is a pushlist pop-up sequence */bool Ispoporder (int *pushlist,int *poplist,int len,int) {if P Oplist==null | | Pushlist==null | |  
      
    LEN&LT;1) return false;  
    int i;  
    int pushindex = 0; for (i=0;i<len;i++) {while (Top==-1 | | stack[top]!= poplist[i]) {//If no elements can be pus  
            H, it shows that does not conform to if (Pushindex = len) return false;  
        Push (stack,pushlist[pushindex++]);  
     }   Pop ();  
return true;  
    int main () {int n; int Stack[max];  
        Secondary stack while (scanf ("%d", &n)!= EOF) {int *pushlist = (int *) malloc (n*sizeof (int));  
        if (pushlist = NULL) exit (exit_failure);  
        int i;  
      
        for (i=0;i<n;i++) scanf ("%d", pushlist+i);  
        int *poplist = (int *) malloc (n*sizeof (int));  
        if (poplist = NULL) exit (exit_failure);  
      
        for (i=0;i<n;i++) scanf ("%d", poplist+i);  
        if (Ispoporder (pushlist,poplist,n,stack)) printf ("yes\n");  
      
        else printf ("no\n");  
        Free (pushlist);  
        Pushlist = NULL;  
        Free (poplist);  
    Poplist = NULL;  
return 0; }

Author: csdn Blog Lan pavilion Wind and Rain

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.