Pop Sequence题目解答-Java实现

编程题 Pop Sequence

题目

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

Sample Input:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample Output:

YES
NO
NO
YES
NO

题目的大意就是说,给你第一行数据分别表示栈的容量,入栈数的总数,那些数按顺序入栈,需要判断的sequence的数目,让你判断下面的那些sequence是不是可能的出栈序列,如果是,输出 YES,否则,输出 NO。

乍一看的时候一点思路都没有,然后开始在草稿纸上写,想着如果是错误的出栈序列该满足什么,然后发现第一个数据必须是 <= 栈的容量的,第二个应该是 <= n+1的,然后后面的数就判断不出来了,无奈之下去网上找了找大神的解答,终于有了点思路。参照博客

总体思路就是按照给你的序列,模拟一个栈,一个个出栈比较,如果错误,就判断 NO,否则判断 YES。更具体的来说,按照给你的信息,构造一个容量和输入的第一行信息所给的一样的栈,然后一行一行地,对数据进行判断:当栈为空或者栈不空,但是栈顶元素小于序列对应的元素且栈未满,就按顺序把数字入栈,然后判断栈顶元素和序列对应的元素,如果相等,说明正确,出栈;如果栈顶大于对应元素,说明不可能出现对应出栈队列,直接判断为 NO;如果栈顶小于对应元素但是栈已经满了,说明不可能再将其入栈,也不会出现所示序列,直接判断为 NO。

比如说序列 3 2 1 7 5 6 4 ,第一个数据是3,此时栈为空,按顺序把1 2 3入栈,之后栈顶为3等于3,出栈,继续,2等于2,出栈,继续,1等于1,出栈,此时栈为空了,遇到7,接着顺序,把4 5 6 7入栈,7等于7,出栈,继续,6大于5,说明栈不可能弹出5,所以判断为 NO。

完整代码如下:

import java.util.Scanner;
public class PopSequence {

    private class Stack{
        int N;
        Node top;
        class Node{
            int data;
            Node next;
        }

        int length(){
            return N;
        }

        boolean isEmpty(){
            return top==null;
        }

        int top(){
            return top.data;
        }

        void push(int data){
            Node oldTop = top;
            top = new Node();
            top.data = data;
            top.next = oldTop;
            N = N + 1;
        }

        int pop(){
            int data = top.data;
            top = top.next;
            N = N - 1;
            return data;
        }
    }

    public void start(){
        Scanner in = new Scanner(System.in);
        Stack s;
        String[] line = in.nextLine().split(" ");
        int beginNum;
        int stackSize = Integer.parseInt(line[0]); // 栈的大小
        int sumOfNum = Integer.parseInt(line[1]); // 数的总数
        int sequences = Integer.parseInt(line[2]); // 需要判断的队列总数
        boolean[] judge = new boolean[sequences]; // 对整个sequence的判断结果

        A:for (int i=0; itemp){
                    judge[i] = false;
                    continue A;
                }
            }
            judge[i] = true;
        }

        for (int i=0; i

你可能感兴趣的:(Pop Sequence题目解答-Java实现)