前言
在准备考研复习专业课数据结构,所以我在mooc平台上找了浙江大学的数据结构课程重新学习,感觉老师留的课后题都很有趣,所以想记录下自己的学习过程。
题目:
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:
1 | 5 7 5 |
Sample Output:
1 | YES |
按照惯例先秀一下AC的图

题解
题目大意
限定堆栈的最大容量M,然后给出一个从1到N的序列按顺序依次入栈;我们可以随机的选择出栈的时刻。要求判断给出的K个出栈序列是否合法。(M,N,K都不超过1000)
假如M=5,N=7,那么 1, 2, 3, 4, 5, 6, 7 这个出栈序列是合法的,而3, 2, 1, 7, 5, 6, 4 这个出栈序列则是非法的。因为当7出栈后,栈顶元素应该为6,但是此序列给出的下一个出栈元素为5,故此序列不合法。
题目分析
我的思路是这样的,首先我将每一个出栈序列都按顺序保存下来,然后建立一个空堆栈,首先将堆栈里push一个0进去。于是我就可以拿栈顶的元素和保存下来的出栈序列作比较,从序列的第一个元素一直比较到第K个元素,每次比较有三种情况
①如果栈顶元素<出栈序列中的第i个元素,那么就将push(栈顶元素+2),若push失败,说明超出堆栈的范围,直接break,此序列不合法。
②栈顶元素=出栈序列中的第i个元素,那么就把栈顶元素pop出来,并且i=i+1
③栈顶元素>出栈序列中的第i个元素,说明序列不合法,直接break退出循环。
循环从1执行到N,循环结束,若没有①②这两种情况则说明,此出栈序列为合法的出栈序列。
完整代码
1 |
|









