04 Pop Sequence (java实现,附图文解析)

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

popsequence题目要求(以7为例)

			1、给定一组数Aaary为 1 2 3 4 5 6 7
			2、给定5行数,判断每一行能否按照规定的顺序出栈

解题思路:

			1、设定一变量为beginNum,从Array中的1~7循环;设置一个栈为st,将beginNum进行入栈、出栈操作(如下图所示)

在这里插入图片描述
2、取5行数中的第二行3 2 1 7 5 6 4,做数组A,则A[i],i=1~7;此数组仅与beginNUm进行比较,不会进出栈(如下图所示)
在这里插入图片描述
3、判断A[i]与beginNum的大小,以及栈st是否为空
若栈st为空时,没法进行比较,只能往栈st中添加beginNum;或者st栈顶元素比A[i]小,并且st栈未满,则继续往st栈中添加beginNum(如下图所示)
在这里插入图片描述
若栈顶元素与A[i]相等,将栈顶元素抛出(如下图所示)
在这里插入图片描述
若栈顶元素比A[i]小,且此时栈满了,或者栈顶元素大于A[i],则此行元素出栈顺序不会符合给定数组A,输出NO
在这里插入图片描述

import java.util.*;

public class PopSequenceDemo
{
	public static void main(String[] args) 
	{
		/**
			popsequence题目要求(以7为例)
				1、给定一组数为 1 2 3 4 5 6 7
				2、给定5行数,判断每一行能否按照规定的顺序出栈
			
			解题思路:
				1、设定一变量为beginNum,从1~7循环;设置一个栈为st,将beginNum进行入栈、出栈操作
				2、取5行数中的第一行,做数组A,则A[i],i=1~7;此数组仅与beginNUm进行比较,不会进出栈
				3、判断A[i]与beginNum的大小,以及栈st是否为空
					若栈st为空,或者st栈顶元素比A[i]小,并且st栈未满,则继续往st栈中添加beginNum
					若栈顶元素与A[i]相等,将栈顶元素抛出
					若栈顶元素比A[i]小,且此时栈满了,或者栈顶元素大于A[i],则此行元素出栈顺序不会符合给定数组A,输出NO
		*/
		
		
		Scanner scan = new Scanner(System.in);
		String[] line = scan.nextLine().split(" ");
		int stacksize = Integer.parseInt(line[0]);		//栈的大小
		int sumOfNum = Integer.parseInt(line[1]);		//每行输入的数的个数
		int sequences = Integer.parseInt(line[2]);		//输入队列数
		boolean[] judge = new boolean[sequences];		//队列的判断结果
		
		Stack<Integer> st;
		int beginNum;							//进行对比的数组 固定为1 2 3 4 5 6 7

		A: for(int i=0; i<sequences; i++){		//将输入的每一行队列进行判断
			st= new Stack<Integer>();
			beginNum = 1;
			line = scan.nextLine().split(" ");		//开始接收每行数据

		//开始遍历每行数据
			for(int j=0; j<sumOfNum; j++){
				int temp = Integer.parseInt(line[j]);

				while(st.empty() || st.peek()<temp&&(st.size()<stacksize)){
					st.push(beginNum++);
				}

				if(st.peek() == temp){
					st.pop();
					continue;
				}

				if(st.peek()<temp&&(st.size()==stacksize) || st.peek()>temp){
					judge[i] = false;
					continue A;
				}
			}
			judge[i] = true;
		}
        
		for(int i=0; i<sequences; i++){
			if(judge[i]){
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值