浙大数据结构课后习题 练习二 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 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 分)的更多相关文章
- 浙大数据结构课后习题 练习二 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 ...
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 浙大数据结构课后习题 练习三 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 ...
- PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)
PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称P ...
- PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)
PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分) 每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...
- PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)
PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分) 在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...
- PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)
PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...
- 数据结构练习 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 ...
- 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总
大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...
随机推荐
- SELECT * 测试
描述 大家通常禁止在生产环境直接使用select * 已成常识了,也常常在开发规范中就会规定不允许直接使用select *,那么我们为什么不允许使用select * ,在一些什么场景下select * ...
- Linux安装Vmware Tools/vmtools(通用)
以下方法适用于centos/Ubuntu #新建一个临时目录,用于挂载光驱 mkdir /tmp/cdrom mount -t iso9660 /dev/cdrom /tmp/cdrom cp -r ...
- C/C++之编程语言学习资源
前言 因朋友相问,藉以帮助需要学习C.C++语言的后来小伙伴. 网络视频资源 选择其中一系列即可,切忌贪多嚼不烂. [系列1:可能会面临开课时间错过的问题,二门课程只要能上其一即可,均为浙大翁恺老师的 ...
- 用Tcpdump抓包
在安卓手机上抓包 1.将手机root并连上,记得开启开发者选项,并选择传输文件 2.将tcpdump程序拷到手机里面,可以直接在电脑上操作,也可以用adb 3.使用adb操作手机内核,安卓内核基于li ...
- Day05:集合操作——线性表(二) / 查找表 / 文件操作——File(一)
文件操作:https://www.cnblogs.com/wsnb8/p/11403626.html
- 通过id()函数学习python的数据存储以及引用方式
id()函数是python的内置函数,用于获取对象的内存地址. 1.1 可以看出,33被存储在内存地址19877464上,对变量a赋值,实际上是将其指向存储着33的内存地址. 1.2 不仅是数字类型, ...
- Python面试-websocket及web框架
一.Websocket 1. websocket概念 在讲websocket之前,我们先来看看ajax轮询和long poll的实现机制. A. ajax轮询 ajax轮询的原理非常简单,让浏览器隔 ...
- JAVA SQL注入漏洞挖掘
java中sql注入主要发生在model层,黑盒测试sql注入的方法结合两点:1,异常注入后,界面有无明显的aql异常报出.2,查看数据库日志是否有脏数据注入. preparestatement方法是 ...
- Java中流的操作练习
文件中的学生信息 学生信息存储在TXT文件中,我们需要对信息进行基本的,增.删.改.查,全部显示操作. 1.学生类/Student package com.yujiao.student; public ...
- linux c++模拟简易网络爬虫
/* * To change this license header, choose License Headers in Project Properties. * To change this t ...