题目

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 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<vector>//前两位不能有6,7,7不能在5,6之前
#include<stack>
using namespace std;
int main(){int m,n,k;cin>>m>>n>>k;for(int i=0;i<k;i++){bool flag=false;stack<int> s;vector<int> v(n);//0~size-1for(int j=0;j<n;j++){scanf("%d",&v[j]);}int index=0;for(int j=1;j<=n;j++){s.push(j);if(s.size()>m) break;while(!s.empty()&&s.top()==v[index]){s.pop();index++;}}if(index==n) flag=true;if(flag) printf("YES\n");else printf("NO\n");}return 0;
}

这题想不到具体实现,还是参考柳神的,等手机好了,我要去扫她的红包:
https://www.liuchuo.net/archives/2232

其他思路可以看这边:https://www.nowcoder.com/questionTerminal/597a931ab1794139835ad2991faeab2d

PAT甲级1051 栈的模拟相关推荐

  1. PAT甲级1051 Pop Sequence:[C++题解]模拟栈、判断序列是否是合法的出栈序列

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 题意:将1~N压栈,判断给定序列是否是合法的出栈序列. 对于序列1~N中的每个值i,先将其压入栈.然后对于它就有两种处理方法:要么压 ...

  2. PAT甲级 1042 Shuffling Machine 模拟洗牌 map的使用

    Solution: 题目要求:要去实现一个模拟洗牌的程序.具体要求如下: (1)初始的排序已经给出. (2)要求洗k次牌,并输出最终排序. (3)给出54个数字,如果第i个位置上的数字为j,就要把第i ...

  3. PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历

    文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...

  4. **PAT_甲级_1057 Stack (30分) (C++)【字符串处理/栈的模拟/树状数组】

    目录 1,题目描述 题目大意 2,思路 数据结构 函数讲解 1,void update(int x, int v): 2,int getsum(int x): 3,void PeekMedian(): ...

  5. PAT甲级1069 The Black Hole of Numbers:[C++题解]模拟、6174

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 注意特判 6174这个数:if(n == 6174) printf("7641 - 1467 = 6174\n" ...

  6. PAT甲级1042 Shuffling Machine:[C++题解]模拟、哈希表、洗牌机

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:序列置换. 这里用到函数memcpy()用于数组复制,用法memcpy(dest, src, sizeof dest); 把src数组 ...

  7. PAT甲级1031 Hello World for U:[C++题解]模拟、数学运算

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:n=n1+n2+n3−2,且n1=n3,n = n_1 +n_2 +n_3-2,且n_1=n_3,n=n1​+n2​+n3​−2,且n ...

  8. PAT甲级1011 World Cup Betting:[C++题解]模拟

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:每场取最大的数. ac代码 #include<bits/stdc++.h> using namespace std;int ...

  9. PAT甲级1008 Elevator:[C++题解]模拟

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:统计上升次数.下降次数:停留次数就是n.然后算数即可. ac代码 #include<bits/stdc++.h> usin ...

最新文章

  1. phpstudy2014 php7.0,phpstudy下载_phpStudy下载2014 官方版_php环境集成包 1.0_零度软件园...
  2. C# 笔记 获取程序当前目录
  3. 主分支和'起源/主人'有分歧,如何'疏远'分支'?
  4. 一篇文读懂分布式系统本质:高吞吐、高可用、可扩展
  5. setactive隐藏之后无法显示_WPS表格:如何显示和隐藏顶部工具栏?
  6. 【MM模块】Contract 采购合同简介
  7. matlab图片压缩
  8. python采用强制缩进if_Python缩进和选择解析
  9. 软件测试qa等级考核制度,QA质量规范
  10. c++框架有哪些_平顶彩钢活动房特点以及用途有哪些?
  11. 大图社区搜索的调查综述(二)——预备知识
  12. sql语句中一些特殊字符的处理
  13. Python接通图灵机器人
  14. thrift之TTransport层的堵塞的套接字I/O传输类TSocket
  15. android动画有几种,Android中常用的几种动画(总结)
  16. sql 新建表 alter_SQL ALTER表
  17. [转]C#:手把手教你用C#打包应用程序(安装程序卸载程序)
  18. 集结六大行业领袖,「数据科学家」新课全球首发!
  19. 微信编辑器有哪些呢?
  20. mp4如何转换成wmv格式

热门文章

  1. 下拉推广系统立择火星推荐_【电商干货】拼多多搜索推广 如何选对致命关键词 ?...
  2. php mail执行命令,PHPMailer 命令执行 任意文件读取漏洞利用 【含POC】
  3. static_cast与dynamic_cast转换
  4. Python Imaging Library: ImageEnhance Module(图像增强模块)
  5. java i%10==3_Java学习笔记(三)
  6. 2017-11-17 为Python添加中文关键字 1
  7. 用于机器学习的Python和HDFS
  8. JavaScript的NaN-唯一 一个自己不等于自己的对象!!
  9. VWware Vsphere 实战记录一、vsphere简介及相关名词介绍
  10. LVS--DR模型介绍及实现