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
#include<cstdio>
#include<stack>
using namespace std;
const int maxn = ;
int a[maxn];
stack<int> s;
int main(){
int m,n,k;
scanf("%d%d%d",&m,&n,&k);
while(k--){
while(!s.empty()){
s.pop();
}
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
int current = ;
bool flag = true;
for(int i = ; i <= n; i++){
s.push(i);
if(s.size() > m){
flag = false;
break;
}
while(!s.empty() && a[current] == s.top()){
s.pop();
current++;
}
}
if(s.empty() && flag == true) printf("YES\n");
else printf("NO\n");
}
return ;
}

02-线性结构4 Pop Sequence (25 分)的更多相关文章

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

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence   (25分) Given a stack wh ...

  2. 02-线性结构4 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 ...

  3. pat02-线性结构4. Pop Sequence (25)

    02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  4. 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 ...

  5. PAT 1051 Pop Sequence (25 分)

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

  6. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

  7. 数据结构练习 02-线性结构3. 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 ...

  8. 浙大数据结构课后习题 练习二 7-3 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 ...

  9. 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 ...

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

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

随机推荐

  1. Spring.net方法的替换

    .为什么有时候你再执行某个方法的时候比如某个操作 a.权限验证 b.任务执行 当我执行到这个方法的时候,我可以先验证权限,如果验证不通过则替换到另一个方法去执行 public class MyValu ...

  2. linux c MQTT客户端实现

    linux c MQTT客户端实现 摘自:https://www.jianshu.com/p/d309de966379 一.前言:mqtt协议是轻量级的消息订阅和发布(publish/subscrib ...

  3. Part2_lesson3---ARM寄存器详解

    进入到ARM Architecture Reference Manual这个文档里面的A2.3 Registers R13在程序中通常用于充当SP堆栈指针的!! R14在程序当中通常用于充当LR(链接 ...

  4. python操作mysql数据库系列-操作MySql数据库(二)

    接口测试框架层级目录结构示意图: page目录下面的mysqlTest.py:存放的是mysql的操作代码 utils目录下面的helper.py:存放的是公共的配置方法 log目录log.md:存放 ...

  5. ceph中查看一个rbd的image的真实存储位置

    1.新建一个image存储 rbd create hzb-mysql --size 2048 2.查看hzb-mysql的所有对象 一个rbd image实际上包含了多个对象(默认情况下是image_ ...

  6. 使用火蜘蛛采集器Firespider采集天猫商品数据并上传到微店

    有很多朋友都需要把天猫的商品迁移到微店上去.可在天猫上的商品数据非常复杂,淘宝开放接口禁止向外提供数据,一般的采集器对ajax数据采集的支持又不太好. 还有现在有了火蜘蛛采集器,经过一定的配置,终于把 ...

  7. 编写高质量代码改善C#程序的157个建议——建议111:避免双向耦合

    建议111:避免双向耦合 双向耦合是指两个类型之间相互引用.下面的代码是一种典型的双向耦合: class A { private B b; public void MethodA() { b.Meth ...

  8. win32 zbar

    一.zbar官方介绍 ZBar 是款桌面电脑用条形码/二维码扫描工具,支持摄像头及图片扫描,支持多平台,例如 iPhone,Andriod 手机,同时 ZBar封装了二维码扫描的 API 开发包. Z ...

  9. ubuntu eclipse 集成pyDev

    Eclipse help 选择安装新软件 添加一个pydev 名字随意.地址是 http://pydev.org/updates. 下面的列表会出现很多PyDev For Eclipse 选择版本最高 ...

  10. sonar安装问题记录

    1.启动时日志中提示 Caused by: java.lang.RuntimeException: can not run elasticsearch as root 错误原因:因为安全问题elast ...