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 ...
随机推荐
- 主流列式数据库评测:InfiniDB
).本文测试的InfiniDB版本是2010年12月20日发布的2.02版,下载文件名分别为InfiniDB64-2.0.2-2.exe 和InfiniDB64-ent-2.0.2-2.exe.安装文 ...
- 队列顺序存储 - 设计与实现 - API函数
队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! queue常用操作 销毁队列 清空队 ...
- HBase中创建索引
hbasene(https://github.com/akkumar/hbasene)是开源项目,在hbase存储上封装使用Lucene来创建索引,代码API非常简单,熟悉lucene的朋友可以很方便 ...
- javascript中的instanceof运算符
instanceof运算符希望左操作数是一个对象,右操作数表示对象的类:如果左侧的对象是右侧类的实例,则返回true,否则返回false.由于js中对象的类是通过初始化它们的构造函数来定义的,因此in ...
- C# / VB.NET合并PDF指定页
在前面的文章中,我们已经知道如何合并.拆分多个PDF文件,在这篇文章中的合并.拆分PDF文档主要是以方便文档管理的目的来操作文档,在文档查阅.管理及存储上很方便实用.但是我们如果想要合并多个文档中的部 ...
- CoolBlog开发笔记第5课:请求与响应
教程目录 1.1 CoolBlog开发笔记第1课:项目分析 1.2 CoolBlog开发笔记第2课:搭建开发环境 1.3 CoolBlog开发笔记第3课:创建Django应用 1.4 CoolBlog ...
- java并发包分析之———ConcurrentSkipListMap
一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSki ...
- ScrollView的顶部下拉和底部上拉回弹效果
要实现ScrollView的回弹效果,需要对其进行触摸事件处理.先来看一下简单的效果: 根据Android的View事件分发处理机制,下面对dispatchTouchEvent进行详细分析: 在加载布 ...
- access按钮事件在子窗体打开窗体或报表
Private Sub Com1_Click()Me.win.SourceObject = "窗体1"End Sub Private Sub Com2_Click()Me.win. ...
- SpringMVC:数据绑定入门(-)
1.数据类型,可以绑定基本数据类型,如int age,或者包装类型如:Integer age; 两者的区别:int 类型时,必填该参数,Integer 可以为空. 2.绑定数组 , 3.绑定对象. 3 ...