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. 在IOS10系统中无法定位问题

    果是非https网页,在http协议下通过html5原生定位接口会返回错误,也就是无法正常定位到用户的具体位置,而已经支持https的网站则不会受影响. 目前提供的解决方案: 1.将网站的http设置 ...

  2. 浏览器提示:源映射错误:request failed with status 404 源 URL:http://xxx.js 源映射 URL:jquery.min.map

    浏览器 jquery1.9.1min.js 报脚本错误 无jquery.min.map 文件 最近在浏览个人网站的时候就遇到了这个问题 我先说一下什么是source map文件. source map ...

  3. 视频网站大杂烩--HTML+CSS练手项目1【Frameset】

    [本文为原创,转载请注明出处] 技术[CSS+HTML]   布局[Frameset] -------------------------------------------------------- ...

  4. Oracle与SQL Server等数据库的区别

    Oracle与SQL Server等数据库的区别 在Oracle中提倡使用一个连接 Oracle处理多个并发语句使用一个连接,大大提升系统能支持的并发量 Oracle运行在32为单进程平台上SGA和P ...

  5. CentOS - Eclipse安装Shelled

    一,下载Shelled: https://sourceforge.net/projects/shelled/ 二,打开Eclipse,以离线方式安装: Help->Install New Sof ...

  6. 重启Kubernetes Pod的几种方式

    方法1 kubectl scale deployment XXXX --replicas=0 -n {namespace} kubectl scale deployment XXXX --replic ...

  7. vip视频播放

    插件  Tampermonkey https://greasyfork.org/zh-CN

  8. sublime text3常用的一些快捷键

    --------------------------------下面的内容可以打印出来贴在电脑旁提醒自己-------------------- Ctrl + Shift + D  快速复制当前的一行 ...

  9. InitContainer

    InitContainer 初始化容器 在Pod中,容器是共享存储资源和网络资源的,所以Init Container容器产生的数据是可以被其他容器作用到的.初始化容器有点类似于postStart 钩子 ...

  10. zsh of termux

    termux-ohmyzsh script :sh -c "$(curl -fsSL https://github.com/Cabbagec/termux-ohmyzsh/raw/maste ...