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. UVa 3211 Now or later (二分+2-Sat)

    题意:有 n 架飞机,每个飞机早着陆,或者晚着陆,让你安排一个方式,让他们着陆的时间间隔尽量大. 析:首先对于时间间隔,可以用二分来解决,然后就成了一个判定性问题,然后怎么判断该时间间隔是不是成立呢, ...

  2. up7-文件保存位置

    asp.net 默认位置:项目/upload/年/月/日/guid/ 代码截图: 位置截图:   jsp 默认位置:tomcat/webapps/Uploader7Oracle/upload/年/月/ ...

  3. JavaScript中两种类型的全局对象/函数【转】

    Snandy Stop, thinking is the essence of progress. JavaScript中两种类型的全局对象/函数 这里所说的JavaScript指浏览器环境中的包括宿 ...

  4. 那些原生的javascript APIs

    在前端开发的时候,我们往往会使用javascript 框架,使用框架的好处多多,提供的方便的操作函数,类继承机制,MV*等,让我们能够快速开发,然而我们应该清楚这些框架都是基于浏览器原生api的封装, ...

  5. Ubuntu下安装配置java及环境变量

    这里的办法不是在线安装,因为需要更新源(你懂的,费时费事~),so这里介绍在Ubuntu上手动下载安装配置Java环境变量 *系统:Ubuntu 16.4 1.下载jdk,直接用系统的Firefox浏 ...

  6. hive(在大数据集合上的类SQL查询和表)学习

    1.jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true&characterEncoding=UTF-8&use ...

  7. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

  8. 走进AOP In .NET

    AOP是什么,有的译为面向方面编程,有的译为面向切面编程,我更加赞同后者.接下来,让我们一步步揭开AOP的面纱. AOP衍生自 编程中出现了一些用OOP技术无法捕捉和解决的问题.跟其他开发工具和思想一 ...

  9. iOS AppStore个人开发者账号申请

    一.申请Apple Developer账号 1.注册App ID 1.打开苹果开发者网页,选择Account,注册Apple ID.   2.填写注册信息 3.地区选择China,填写好验证码,点击C ...

  10. python---Numpy模块中数组运算的常用代码示例

    import numpy as np # Numpy数组操作 print('========访问列表元素, 切片,赋值===========') arr = np.array([2., 6., 5., ...