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<iostream>
#include<stack>
using namespace std; const int maxn = ; int check(int *a,int n,int m); int main()
{
int arr[maxn] = {};
int n,m,k;
scanf("%d%d%d",&m,&n,&k); for (int i = ; i < k; i++)
{
for (int j = ; j <= n; j++)
{
scanf("%d",&arr[j]);
}
if(check(arr,n,m))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
} int check(int *a,int n,int m)
{
int iRet = -;
stack<int> s;
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())
{
current++;
s.pop();
}
} if (flag && s.empty())
{
iRet = ;
}
else
{
iRet = ;
} return iRet;
}

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. pat02-线性结构4. Pop Sequence (25)

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

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

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

  5. 线性结构4 Pop Sequence

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

  6. 数据结构练习 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 ...

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

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

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

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

随机推荐

  1. windows上git clone命令速度过慢问题的解决

    在windows上用git clone 命令克隆一个仓库,速度非常的慢,但是浏览器访问github的速度确挺正常的,我也用了翻墙软件(SSR). git设置一下全局代理可以解决这个问题: git co ...

  2. Privoxy搭建代理服务器

    Privoxy搭建代理服务器 Docker Hub镜像地址 Dockerfile FROM alpine EXPOSE 8118 RUN apk --no-cache --update add pri ...

  3. pandas-11 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely错误解决方法

    pandas-11 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be sa ...

  4. PHP7中方法的弃用

    php7与数据库连接创建函数方法调用: function fun_conn($sql) { $con = mysqli_connect("localhost", "roo ...

  5. 车间管理难?APS系统为你智能排程

    对 APS系统不熟或者不了解他的一些运行规则也是在实施项目中导致经常不能正常运行不可忽视的因素,对 APS系统的早期了解是整个项目实施运行的成功至关重要的因素. 如果不了解 APS潜在的因素和运行准则 ...

  6. FIneUICore 版本的 AppBoxMvcCore

    http://www.51aspx.com/code/codename/64088 CORE版本的APPBOXMVC欢迎下载

  7. scrapy xpath去除空格

    content = response.xpath("normalize-space('//img/@src')")

  8. Android笔记(七十六) 点菜DEMO

    一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...

  9. 利用ansible书写playbook搭建HAProxy+Keepalived+PXC负载均衡和高可用的PXC环境续

    ansible.playbook.haproxy.keepalived.PXC haproxy+keepalived双主模式调度pxc集群 HAProxy介绍 反向代理服务器,支持双机热备支持虚拟主机 ...

  10. C++(五十) — 容器中元素满足的条件

    容器中的内容必须满足三个条件: (1)无参构造函数 (2)拷贝构造函数 (3)重载 = 运算符 #define _CRT_SECURE_NO_WARNINGS #include <iostrea ...