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;
int main(){
//最大容量,一行最大数,n行
int maxCapacity,maxNum,line;
cin>>maxCapacity>>maxNum>>line;
while(line--){
stack<int> sta;
int val[maxNum];int a=;
for(int i=;i<maxNum;i++){
cin>>val[i];
}
bool no=false;
for(int i=;i<maxNum;i++){
sta.push(i+);
if(sta.size()>maxCapacity){
no=true;
break;
}
while(!sta.empty()&&val[a]==sta.top()){
sta.pop();
a++;
}
}
if(no) cout<<"NO"<<endl;
else if(sta.empty()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
system("pause");
return ;
}

浙大数据结构课后习题 练习二 7-3 Pop Sequence (25 分)的更多相关文章

  1. 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  2. 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  3. 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  4. PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)

    PTA数据结构与算法题目集(中文)  7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分)   计算机程序设计能力考试(Programming Ability Test,简称P ...

  5. PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)

    PTA数据结构与算法题目集(中文)  7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分)   每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...

  6. PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)

    PTA数据结构与算法题目集(中文)  7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分)   在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...

  7. PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)

    PTA数据结构与算法题目集(中文)  7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分)   胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...

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

  9. 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总

    大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...

随机推荐

  1. SQL学习(二)SQL基础的增删改查

    在测试时使用数据库时,用的比较多的就是增删改查SQL了. 一.增加(insert into ...values) 用于向表中插入新记录 1.不指定列(表示:依次插入所有列的值) insert into ...

  2. stegsolve---图片隐写查看器

    今天做CTF隐写术的题偶然发现一隐写图片查看的神器------stegsolve,分享给大家 stegsolve下载地址:http://www.caesum.com/handbook/Stegsolv ...

  3. kubernetes学习:CKA考试题

    1. 列出环境内所有的pv 并以 name字段排序(使用kubectl自带排序功能) kubectl get pv --sort-by=.metadata.name 2. 列出指定pod的日志中状态为 ...

  4. (3)TinyXml2的详解及使用

        TinyXml2是开源的c++xml文件解析库,简单实用,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作.   (1)DOM DOM(Document Object Mode ...

  5. Delphi动态创建菜单

    在程序运行中动态创建菜单,主要使用TMeunItem类,所有菜单的条目都是TMenuItem的一个实例. 打开Delphi7集成开发环境,在默认新建工程里,放置一个Button1按钮和MainMenu ...

  6. 行为验证码的asp.net MVC实现方式 qq521877626

    界面http://localhost:你的服务器/Code/index 实现步骤: 注册账号https://www.geetest.com   新增验证 下载demo (url:http://docs ...

  7. 基于vs插件的abp代码生成器

    工作了这么多年,一直都在小公司摸爬滚打,对于小公司而言,开发人员少,代码风格五花八门.要想用更少的人,更快的速度,开发更规范的代码,那自然离不开代码生成器.之前用过动软的,也用过T4,后面又接触了力软 ...

  8. Mybatis(三) 动态SQL

    if + where 用法 1. if 元素来实现多条件查询 1.1 UserMapper.xml配置文件 <!--查询用户列表 (if)--> <select id="g ...

  9. 实现一台Linux电脑连接另一台Linux(SSH实现linux之间的免密码登陆)

    怎么实现一台Linux电脑连接另一台Linux电脑? 首先查看是否安装ssh服务:systemctl status sshd.service 启动服务:systemctl start sshd.ser ...

  10. Git服务器搭建与配置管理

    Git for Windows TortoiseGit:是一个开放的git版本控制系统的源客户端,支持Winxp/vista/win7.离不开真正的Git(Git for windows版本名字又叫M ...