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 ...
随机推荐
- Gradle 1.12用户指南翻译——第三十章. CodeNarc 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- 芯片SIAT-002测试PCB板设计
这个板子,从原理图到PCB板,总共画了6天,接近一个星期!虽然说各种麻烦,但总算学到了一些新知识.谨记以备后查. 附注: 模拟地与数字地详解 单片机晶振电路 1. 走线规划 针对采用BGA封装及引脚数 ...
- RTMPdump(libRTMP) 源代码分析 7: 建立一个流媒体连接 (NetStream部分 2)
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
- Gradle 1.12用户指南翻译——第四十四章. 分发插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- 对于CocoaPods的简单理解,实践安装使用过程和常见问题
(本文是自己通过其他文章进行的自我编辑和简单修改,请大家凑活看看) 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发iOS项目不可避免地 ...
- Objective-C 是如何慢慢走红的?
对大多数人来说,Objective-C都是陌生的,原因在于它的走红过程太短.但透过Objective-C的火爆,我们就不难理解苹果程序商店的应用程序为什么轻易就突破了十万个. Objective-C的 ...
- 快速熟悉Oracle索引
一.索引 1.1 什么是索引? 一种用于提升查询效率的数据库对象: 通过快速定位数据的方法,减少磁盘的输入输出操作: 索引信息与表独立存放: Oracle数据库自动使用和维护索引. 1.2 索引分类 ...
- asp.net 调试与iis部署的问题
第一个问题:编译器错误信息: CS0016: 未能写入输出文件"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET ...
- windows from 手风琴
public class OutlookBar : Panel { private int SelectedBandHeight { get; set; } public int ButtonHeig ...
- AngularJS:何时应该使用Directive、Controller、Service?
AngularJS:何时应该使用Directive.Controller.Service? (这篇文章你们一定要看,尤其初学的人,好吗亲?) 大漠穷秋 译 AngularJS是一款非常强大的前端MVC ...