02-线性结构4 Pop Sequence (25分)

02-线性结构4 Pop Sequence   (25分)
Given a stack which can keep MM numbers at most. Push NN numbers in the order of 1, 2, 3, ..., NN 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 MM is 5 and NN 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): MM (the maximum capacity of the stack), NN (the length of push sequence), and KK (the number of pop sequences to be checked). Then KK lines follow, each contains a pop sequence of NN 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
 
时间限制:400ms
内存限制:64MB
代码长度限制:16kB
判题程序:系统默认
作者:陈越
单位:浙江大学
 

题目判定


题意:栈的大小M,输入序列的长度N(默认序列即为1,2,3…,N),入栈出栈的顺序不定。有K个测试序列,判断每一个测试序列是否是可能的出栈顺序。

分析:就是从出栈顺序退出进栈顺序。当我们遇见输出x时,则要考虑的是x前的元素,即小于等于x的元素都先push栈,才会有pop x;1。栈为空时,判断需要填入的数 是否小于 栈的容量(即M)2。若后一个数比前一个数大,又要push其之前的数 再判断 3。若后一个数比前一个数小,则要判断栈顶元素是否与其相等

#include 
#include 
#include 
#include 
#include 

using namespace std;

int M, N, K;

int Check(vector &v)
{
    int i=0;
    int num=1;
    int cap=M+1;
    stack sta;
    sta.push(0);
    while(ista.top() && sta.size() vec(N,0);
    scanf("%d%d%d",&M,&N,&K);

    for(int i=0; i


你可能感兴趣的:(中国大学MOOC-陈越,何钦铭-数据结构-2016秋)