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

题目分析:一道模拟题 写了半天
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
stack<int> S1; int main()
{
int M, N, K;
cin >> M >> N >> K;
vector<int> Array(N+);
vector<int> Test(N+);
for (int i = ; i <= N; i++)
Array[i] = i;
for (int i = ; i < K; i++)
{
while (S1.size())
S1.pop();
for (int j = ; j <= N; j++)
cin >> Test[j];
int k = , t = ,flag=;
while (t<=N)
{
if (S1.size()> M)
{
flag = ;
break;
}
else if (S1.size())
{
if (S1.top() == Test[t])
{
S1.pop();
t++;
}
else if(k<=N)
{
S1.push(Array[k]);
k++;
}
else
{
if (S1.top() == Test[t])
{
S1.pop();
t++;
}
else
{
flag = ;
break;
}
}
}
else {
S1.push(Array[k]);
k++;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}

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

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

  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 (25 分)(栈的模拟)

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

  4. 1051 Pop Sequence (25分)栈

    刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...

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

  7. 【PAT】1051 Pop Sequence (25)(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. 阿里云Tomcat配置

    阿里云Tomcat配置并开放 本文可对以下问题提供参考 服务器 如何配置 Tomcat 配置 端口在监听,但是外网无法访问怎么办 注意事项: 对于阿里云服务器相对较为特殊,因为阿里云服务器除了需要在系 ...

  2. Vue2.0 【第二季】第3节 Vue.set全局操作

    目录 Vue2.0 [第二季]第3节 Vue.set全局操作 第3节:Vue.set全局操作 一.引用构造器外部数据 二.在外部改变数据的三种方法: 三.为什么要有Vue.set的存在? Vue2.0 ...

  3. vquery 一些应用

    // JavaScript Document function myAddEvent(obj,sEv,fn){ if(obj.attachEvent){ obj.attachEvent('on'+sE ...

  4. WEB应用之http协议和httpd闲聊

    什么是web?在日常生活中我们常常听到web这个词,它到底是什么呢?今天我们来聊一聊web应用http协议:相信生活在如今互联网时代的我们,http这个协议应该对我们不是很陌生吧!比如双十一双十二我们 ...

  5. Windows通过VNC连接并显示Linux桌面(Ubuntu16.04)

    目录 Linux中安装VNC服务 Linux中安装桌面环境 Windows中安装VNC Viewer Linux中安装VNC服务 sudo apt-get update sudo apt-get in ...

  6. C语言 变量初始化二进制、八进制、十六进制

    int a1 = 10;   //十进制 int a2 = 0b10;  //二进制 int a3 = 010;    //八进制 int a4 = 0x10;  //十六进制 打印的结果:

  7. 文件上传transferTo一行代码的bug

    本次的项目环境为 Running with Spring Boot v1.5.10.RELEASE, Spring v4.3.14.RELEASE, 服务器环境为CentOS7.0. transfer ...

  8. 覆盖io.spring.platform管理的版本号

    使用io.spring.platform时,它会管理各类经过集成测试的依赖版本号.想要覆盖其中某个依赖的版本号个: https://www.cnblogs.com/ld-mars/p/11818252 ...

  9. javaweb_HTML

    第一章:网页的构成 1.1概念:b/s与c/s 1.1.1 现在的软件开发的整体架构主要分为B/S架构与C/S架构: b/s:浏览器/服务器 c/s:客户端/服务器 客户端:需要安装在系统里,才可使用 ...

  10. bash中的if条件语句报错[: missing `]'

    这是我的一个小demo #!/bin/bash read -p "请输入3个数:" n1 n2 n3 if [ $n1 -gt $n2 ] && [ $n1 -gt ...