OFFER prove safety of the stack push, pop sequence (nine degrees OJ1366)

Subject description:

Two input sequence of integers, the first sequence representing a pressed stack order, determines whether the pop-up sequence for the second order stack. All figures are not pushed onto the stack is assumed equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop, but 4,3,5,1,2 it is impossible to push pop-up sequence of the sequence.

 

Input:

Each test case consists of three lines:

Conduct a first integer n (1 <= n <= 100000), indicates the length of the sequence.

The second line contains n integer representing the order of the stack of press-fitting.

The third line contains n integer representing the order of the pop-up stack.

 

Output:

Corresponding to each test case, if the second sequence is the first sequence of pop-up sequence output Yes, otherwise the 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

Problem-solving ideas:

  Again according to the first set of data on the stack, each stack have to check, whether with the same stack content, if the same, then the stack.

  Finally, if the element does not exist in the stack, the second sequence is demonstrated Stack sequence.

Code:

#include <stdio.h>
#include <stdlib.h>
int arr[100005] = {0};
int test[100005] = {0};
int testStack(int n);
int main(int argc, char const *argv[])
{
    int n,i;
    while(scanf("%d",&n)!=EOF && n>=1 && n<=100000){
        for(i=0;i<n;i++){
            scanf("%d",&arr[i]);
        }
        for(i=0;i<n;i++){
            scanf("%d",&test[i]);
        }
        if(testStack(n))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
int testStack(int n){
    int temp[100005]={0};
    int top=0;
    int j=0;
    int i;
    for(i=0;i<n;i++){
        temp[top++] = arr[i];
        while(top>0 && temp[top-1] == test[j]){
            j++;
            top--;
        }
    }
    if(top)
        return 0;
    else
        return 1;
}
/**************************************************************
    Problem: 1366
    User: xhalo
    Language: C
    Result: Accepted
    Time:230 ms
    Memory:2012 kb
****************************************************************/

 

Reproduced in: https: //my.oschina.net/u/204616/blog/544991

Guess you like

Origin blog.csdn.net/weixin_34023863/article/details/91989462