PAT1051:Pop Sequence
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 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 思路
栈的应用。这里直接用vector模拟栈,不得不说vector太强大了,既可以当栈又可以当队列用。 这道题需要理清楚弹出序列不满足的条件:
1.压进去数字后的栈容量大于限定的容量。
2.弹出序列需求的弹出元素不在栈顶。 简单过程:
1.用一个bool值isSuccess来标识弹出序列是否可行,默认为true。
2.保存第一次压入操作的最大元素cur,然后遍历弹出序列:
1)如果弹出序列的当前元素temp比当前最大元素cur小,则它必须等于此时的栈顶元素top,否则要想得到和temp一样的值,就得不停弹出top,直到top == temp,但显然这样已经和弹出序列不匹配了,不可行,isSuccess置为false。
2)如果temp比最大元素cur更大,则把cur + 1和temp之间的元素压入栈中,并检查此时栈容量是否超过要求,超过表明该序列也不可行,isSuccess置为false。
3.遍历完弹出序列后,根据标识isSuccess的值输出YES or No即可。
代码
#include<iostream>
#include<vector>
using namespace std; int main()
{
int N,M,K;
while(cin >> M >> N >> K)
{
vector<int> stk;
while(K--)
{
bool isSuccess = true;
int curmax = ;
stk.push_back();
for(int i = ; i <= N;i++)
{
int temp;
cin >> temp;
if(temp > curmax)
{
for(int j = curmax + ; j <= temp;j++)
stk.push_back(j);
if(stk.size() > M)
{
isSuccess = false;
}
curmax = temp;
}
else
{
if(stk.back() != temp)
{
isSuccess = false;
}
}
stk.pop_back();
}
if(isSuccess)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
}
PAT1051:Pop Sequence的更多相关文章
- PAT1051. Pop Sequence (25)
#include <iostream> #include <stack> using namespace std; int sizeMax,n,k; stack<int& ...
- 1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...
- 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 ...
- Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分) Question:Given a stack which can keep M numbers at most. Push ...
- 02-线性结构3 Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- Pop Sequence (栈)
Pop Sequence (栈) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...
- 数据结构练习 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 ...
- 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 ...
- A1051. Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
随机推荐
- 本教程针对HBuilder5.0.0,制作日期2014-12-31(从HBuilder工具上获得)
/*注:本教程针对HBuilder5.0.0,制作日期2014-12-31*/创建HTML结构: h 8 (敲h激活代码块列表,按8选择第8个项目,即HTML代码块,或者敲h t Enter)中 ...
- TableLayout和Viewpager实现切换
因为我是在之前的基础上写的,所以这个TableLayout和Viewpager实际上是写在Fragment上的.要写到Activity里其实也是一样的啦. 先看效果图,原谅我不会动态图,只能截个图啦 ...
- 【cocos 2d-x】VS2013+cocos2d-x3.3Final+Adriod交叉编译环境配置(超详细版)
本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder 微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...
- ITU-R BT.1788建议书 对多媒体应用中视频质量的主观评估方法
ITU-R BT.1788建议书 对多媒体应用中视频质量的主观评估方法 (ITU‑R 102/6号研究课题) (2007年) 范围 数字广播系统允许提供多媒体和数据广播应用,包括视频.音频.静态图像. ...
- Git与远程reposiory的相关命令
问题1:Git如何同步远程repository的分支(branch) 某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢? 分析: ...
- 一键安装Android开发环境
一键安装Android开发环境 1 下载tadp-3.0r4-linux-x64.run 进入下面的地址下载: https://developer.nvidia.com/gameworksdownlo ...
- linux下64位汇编的系统调用(3)
背景知识基本交代清楚了,下面我们实际写一个小例子看一下.代码的功能很简单,显示一行文本,然后退出.我们使用了syscall中的write和exit调用,查一下前面的调用号和参数,我们初步总结如下: w ...
- mac os x下的一些小技巧
1显示swap空间: sysctl vm.swapusage 其中sysctl中有很多可以控制和查看的项,可以通过sysctl -A列举,另外可以通过man sysctl来查看. 而实际swap文件和 ...
- 如何获取自己想要模拟的APP的相关图片?
一.首先打开iTunes APP,找到自己想要模拟实现的APP,并下载 二.找到下载的APP在iTunes中的位置 三.选中对应的APP,点击右键选择在Finder中显示,会打开对应的文件窗口,打开对 ...
- 双系统或三系统:Grub Rescue修复方法
我是在装三系统的时候(1.WIN 7 ,2.Ubuntu 12.04 ,3.CentOS 6.4 ),中间步骤出错,造成引导区覆盖,grub乱掉了. 症状: 开机显示:GRUB loading err ...