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. Excel 常用快捷键

    Excel 常用快捷键 1. 移动整列 使用Shift快捷键可以快速移动整列:选中该列,当鼠标变成十字箭头时,按住Shift键,然后将该列移动到想要的位置. 2 绝对引用 使用F4快捷键可以快速设置绝 ...

  2. 认识Web前端、Web后端、桌面app和移动app新开发模式 - 基于Node.js环境和VS Code工具

    认识Web.桌面和移动app新开发模式 - 基于Node.js环境和VS Code工具 一.开发环境的搭建(基于win10) 1.安装node.js和npm 到node.js官网下载安装包(包含npm ...

  3. 实践作业4:Web测试实践(小组作业)每日任务记录5

    (一)今日任务更新 本次小组作业均已完成! 本组文件最终pdf文件(文件稍大,请耐心等待加载):https://files.cnblogs.com/files/ruanshuo170204/Web测试 ...

  4. Python - excel 详解

    安装 pip install xlrd        # 读xlspip install xlwt     # 写xlspip install xlutils     # 改写xls 读取 Excel ...

  5. UVa 12342 Tax Calculator (水题,纳税)

    今天在uva看到一个水题,分享一下. 题意:制定纳税的总额,有几个要求,如果第一个180000,不纳,下一个300000,纳10%,再一个400000,纳15%,再一个300000,纳20%,以后的纳 ...

  6. C#中深复制和浅复制

    C# 支持两种类型:“值类型”和“引用类型”. 值类型(Value Type)(如 char.int 和 float).枚举类型和结构类型. 引用类型(Reference Type) 包括类 (Cla ...

  7. SIP协议整理

    本文记录开发.实现IMS项目时,整理的SIP协议基础知识:若有侵权,请告之. SIP协议 1.      SIP协议简介 SIP是一个应用层的控制协议,可以用来建立.修改.和终止多媒体会话(或者会议) ...

  8. 洛谷P4149 [IOI2011]Race(点分治)

    题目描述 给一棵树,每条边有权.求一条简单路径,权值和等于 KK ,且边的数量最小. 输入输出格式 输入格式:   第一行:两个整数 n,kn,k . 第二至 nn 行:每行三个整数,表示一条无向边的 ...

  9. 电脑连接真机,但是androidstudio不显示手机,ADB Interface黄色感叹号

    1.打开设备管理器 右键计算机 -->管理-->选择设备管理器 2. 步骤如下: 1. 在设备管理器中,找到ADB Interface,右击,选择“更新驱动程序软件” 2. 在弹出的窗口中 ...

  10. C#和JAVA 访问修饰符

    JAVA ----------------------------------------------- 访问修饰符        描述 ------------------------------- ...