PAT A 1051(甲级)

1051 Pop Sequence(25 分)

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

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

 


 

这道题是给出一个序列的数字,在一定条件下,判断是否可以将1 ~ N以这样的顺序出栈。

那么其实我们可以通过模拟整个过程来进行判断,记 j = 1 为当前将要被压入栈的数字,用 i 来代表输入序列的下标:

接下来就会产生非常明显的两种情况,

1.栈为空时,我们将 j++ 压入栈

2.栈不为空时,执行一定判断

执行判断其实就是将栈顶元素和当前的序列元素进行比较:

1.如果序列元素大于栈顶元素,那么我们就可以接着将 j++ 压入栈进行下一次循环

2.如果序列元素小于栈顶元素,这个时候我们可以确定不能以这个顺序出栈了,因为如果栈顶元素大于序列元素,就证明需要输出的序列元素就在栈内,而想要输出这个元素必须先把栈顶的元素输出,此时就不是我们想要的序列,所以也就没有办法以这个顺序出栈了。

3.两者相等,这个是最令人高兴的情况,此时我们将栈顶元素出栈,并且 ++i 检查下一个序列元素

同时不要忘记题目中给定的栈容量和其他限制条件,当stack.size() > 最大容量的时候,也就代表着不能以这个顺序出栈了,造成同样结果的还有什么呢,就是 j > n + 1的时候(为什么是 n + 1 不是 n 呢,因为最后一次吧 j 压入之后还会对 j 进行 +1操作,所以 j 一定会大于 n 但是不大于 n + 1)。

那结束条件是什么呢,就是当检查完序列中最后一个元素的时候,就可以结束了,也就是 i >= n 的时候即返回 true.

 


 

AC代码:

#include <cstdio>
#include <stack>

using namespace std;

int se[1010];

bool correct(int, int);

int main() {
    stack<int> is;
    int m, n, k;

    scanf("%d%d%d", &m, &n, &k);
    for(int i = 0; i < k; ++i) {
        if(correct(m, n)) {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }

    return 0;
}

bool correct(int m, int n) {
    stack<int> is;
    for(int i = 0; i < n; ++i) {
        scanf("%d", se + i);
    }
    int i = 0, j = 1;
    while(1) {
        if(is.empty()) {
            is.push(j++);
        } else {
            if(is.top() < se[i]) {
                is.push(j++);
            } else if(is.top() > se[i]) {
                return false;
            } else {
                is.pop();
                ++i;
            }
        }

        if(is.size() > m) {
            return false;
        }

        if(i >= n) {
            return true;
        }

        if(j > n + 1) {
            return false;
        }
    }
}

 


 

如有错误,欢迎指摘。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值