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

C++代码如下:

 #include<iostream>
#include<stack>
#include<queue>
using namespace std; int main() {
int m, n, k;
cin >> m >> n >> k;
while (k--) {
int temp;
stack<int>s;
queue<int>q;
for (int i = ; i < n; i++) {
cin >> temp;
q.push(temp);
}
for (int i = ; i <= n;i++) {
s.push(i);
if (s.size() > m) break;
while (!s.empty() && ( q.front() == s.top() ) ) {
q.pop();
s.pop();
if (q.empty()) break;
}
}
if (q.empty()) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}

【PAT】1051 Pop Sequence (25)(25 分)的更多相关文章

  1. PAT 1051 Pop Sequence[栈][难]

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

  2. PAT 1051 Pop Sequence (25 分)

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

  3. PAT 1051 Pop Sequence

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; bool p ...

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

  6. PAT Advanced 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 ...

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

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

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

随机推荐

  1. java 调用 C# webapi

    最近项目需要 java调用  .net 的webapi. 对于get来说很简单,但是post方法遇到了些问题,最后也是曲线救国. 先看代码 Java  代码 public static void ma ...

  2. MT【83】三个等号

    分析:此类三个等式的一般做法先记为$t$,则有如下做法:

  3. 自学Zabbix3.5.1-监控项item-key介绍

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.5.1-监控项item-key介绍 个人觉得艰难理解,故附上原文档:https ...

  4. A guess 解题报告

    A guess 题意 选一个\([1,n](n\le 500)\)的整数,可以询问数是否属于区间\([l,r]\),多次询问一起回答,统计有多少种询问区间集合(无序)满足可以猜出这个数,对\(p(2^ ...

  5. 在c语言中嵌入汇编语句,对于我来说相当难。

    今天早上在csdn论坛上看到一个帖子http://topic.csdn.net/u/20120917/14/82f42e17-977a-4824-95bd-7b79db15d283.html:“C语言 ...

  6. Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction 异常一例

    参考下面的文章,最终找到我的报错原因: 我是在 service中一个以 get开头的方法中,加入了一行数据库数据删除代码,因为 spring的事务配置中,配置了get开头的方法 是 readonle的 ...

  7. 包学会之浅入浅出Vue.js:开学篇(转)

    包学会之浅入浅出Vue.js:开学篇 蔡述雄,现腾讯用户体验设计部QQ空间高级UI工程师.智图图片优化系统首席工程师,曾参与<众妙之门>书籍的翻译工作.目前专注前端图片优化与新技术的探研. ...

  8. springSecurity自定义认证配置

    上一篇讲了springSecurity的简单入门的小demo,认证用户是在xml中写死的.今天来说一下自定义认证,读取数据库来实现认证.当然,也是非常简单的,因为仅仅是读取数据库,权限是写死的,因为相 ...

  9. bzoj千题计划241:bzoj3864: Hero meet devil

    http://www.lydsy.com/JudgeOnline/problem.php?id=3864 题意: 给你一个DNA序列,求有多少个长度为m的DNA序列和给定序列的LCS为0,1,2... ...

  10. WHAT I READ FOR DEEP-LEARNING

    WHAT I READ FOR DEEP-LEARNING Today, I spent some time on two new papers proposing a new way of trai ...