题目如下:

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

题目要求判断一个长度为N的出栈序列是否可以用一个1~N的入栈序列和一个容量为M的堆栈完成。

乍一看题目似乎很难入手,但只要仔细思考一下,可以发现其中有很大的规律可循。

我们以3217564为例,一个容量为5的堆栈,因为入栈序列已经确定为1~7,按照下面的步骤思考:

我们设将要入栈的元素为push_index,要输出的元素为x:

x=3,push_index=1,还没有压到3,要输出三,必须一直压到3,然后弹出3,此时push_index=4(下一次要压入的是4),堆栈现在从底到顶分别为12。

接下来x=2,push_index=4,已经无法通过压入元素到达x,因此只有栈顶为x时才可能得到要求的输出序列,这时候判断栈顶是不是x,不是则这个序列无法实现,是则继续判断下一个x。

同样地,x=1也是这样处理,我们发现堆栈中有12,正好输出为21,因此可以正确输出321。

下面x=7,push_index=4,一直压入到x再弹出,则push_index=7,堆栈中在弹出7后为456。

下面x=5,push_index=7,这时候又要判断栈顶是不是5了,发现栈顶是6,已经不可能得到要求的输出序列。

通过这样举例,抽象如下:

①如果当前想要输出的元素x<将要入栈的元素push_index(由入栈序列得到,从1开始),则一直压入到x,然后弹出x,同时push_index指向下一个元素。

②如果当前想要输出的元素x==将要入栈的元素push_index,说明压入再弹出即可,这时候直接把push_index后移继续处理。

③如果当前想要输出的元素x>将要入栈的元素push_index,说明只能通过出栈方式得到x,看栈顶是否是x,是则弹出,继续处理;否则输出NO。

④只有③的条件始终满足,才输出YES。

代码如下:

#include <iostream>
#include <stack>
#include <vector>
#include <stdio.h> using namespace std; int main()
{
stack<int> nums;
int M,N,K;
int push_index = 1; // 压栈序列
int index = 1; // 遍历到的出栈序列位置
int x; // 要找的元素
bool right_flag = false;
cin >> M >> N >> K;
vector<int> pop_sequence(N);
for(int i = 0; i < K; i++){
for(int j = 0; j < N; j++){
cin >> pop_sequence[j];
}
push_index = 1;
index = 1;
while(!nums.empty()) nums.pop();
right_flag = true;
while(index < N){
x = pop_sequence[index - 1];
index++;
if(x < push_index){
if(!nums.empty() && nums.top() == x){
nums.pop();
}else{
right_flag = false;
break;
}
}else if(x == push_index){
push_index++;
}else{ // x < push_index,应该压入到index为止,弹出这个,再向后判断 if(push_index > N) {
right_flag = false;
break;
} for(; push_index <= x; push_index++){
nums.push(push_index);
if(nums.size() > M){
right_flag = false;
break;
}
}
nums.pop();
}
}
if(right_flag) cout << "YES" << endl;
else cout << "NO" << endl;
} return 0;
}

1051. Pop Sequence (25)的更多相关文章

  1. PAT 解题报告 1051. Pop Sequence (25)

    1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...

  2. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

  3. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  4. 【PAT】1051 Pop Sequence (25)(25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  5. PAT Advanced 1051 Pop Sequence (25) [栈模拟]

    题目 Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, -, N and ...

  6. 1051 Pop Sequence (25分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  7. PAT (Advanced Level) 1051. Pop Sequence (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. PAT甲题题解-1051. Pop Sequence (25)-堆栈

    将1~n压入最多为m元素的栈 给出k个出栈序列,问你是否能够实现. 能输出YES 否则NO 模拟一遍即可,水题. #include <iostream> #include <cstd ...

  9. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

随机推荐

  1. python中的缩进问题

    python中没有{}来表示代码块,而是用缩进来表示,刚开始写python代码,没有注意缩进,结果各种报错(( ╯□╰ )). 在python中的原则就是同一层次的代码一定要有相同的缩进!!! 从上图 ...

  2. C语言程序设计第五次作业——循环结构(1)

    一.改错题 1.题目 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增加2℉.计算公式如下:c = 5×( ...

  3. React .js框架的环境搭建

    React学习笔记(一)- 环境搭建   最近在学习react相关的知识,刚刚起步,一路遇坑不断.自己做个笔记,方便日后总结,也供相同趣味的小伙伴一起交流探讨. 学习时主要参考官网的教程:https: ...

  4. python文件转exe

    .py文件转exe文件 1.软件说明: 用python写一个视频处理软件,用到的库是moviepy 2.所用软件: Python 3.6.5 32位 pycharm  专门的python编辑ide,推 ...

  5. MySQL使用判断

    1.case语法 在第一个方案的返回结果中, value=compare-value.而第二个方案的返回结果是第一种情况的真实结果.如果没有匹配的结果值,则返回结果为ELSE后的结果,如果没有ELSE ...

  6. 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00

    spring-data-redis的RedisTemplate<K, V>模板类在操作redis时默认使用JdkSerializationRedisSerializer来进行序列化解决方法 ...

  7. Windows转Linux总结(附带常用Linux命令-LinuxMint)

    这是我在Linux系统下写的第一篇博客,花了一周的时间从Windows系统转到Linux并且可以完成日常操作,能在Linux系统下完成开发,运用各种开发工具,写各种语言小程序和JavaEE. 经过这一 ...

  8. Prometheus(转载)

    Prometheus 系统监控方案 一 https://www.cnblogs.com/vovlie/p/Prometheus_CONCEPTS.html 最近一直在折腾时序类型的数据库,经过一段时间 ...

  9. 数据结构之Treap

    1. 概述 同splay tree一样,treap也是一个平衡二叉树,不过Treap会记录一个额外的数据,即优先级.Treap在以关键码构成二叉搜索树的同时,还按优先级来满足堆的性质.因而,Treap ...

  10. php闭包类外操作私有属性

    Closure::bind() Closure::bindTo(); class person{ private $age; private $sex; public function __const ...