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 ...
随机推荐
- android 常用方法集合
private static Contextcontext; privatestatic Displaydisplay; private static String TAG = "MyToo ...
- 计算机编码方式详解(Unicode、UTF-8、UTF-16、ASCII)
整理这篇文章的动机是两个问题: 问题一: 使用Windows记事本的"另存为",可以在GBK.Unicode.Unicode big endian和UTF-8这几种编码方式间相互转 ...
- adb shell后出现error解决方案
解决办法: 解决办法: 1.adb kill-server 2.adb start-server 3.adb remount 4.adb shell 一般情况下都可以在此启动adb相关
- "《算法导论》之‘栈’":栈的三种实现(静态数组、动态数组及指针)
本文有关栈的介绍部分参考自网站数据结构. 1. 栈 1.1 栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶(Top),另一端称为栈底( ...
- Erlang cowboy 入门参考
Erlang cowboy 入门参考 cheungmine,2014-10-28 本文翻译自: http://ninenines.eu/docs/en/cowboy/HEAD/guide/gettin ...
- 海量数据处理 - 10亿个数中找出最大的10000个数(top K问题)
前两天面试3面学长问我的这个问题(想说TEG的3个面试学长都是好和蔼,希望能完成最后一面,各方面原因造成我无比想去鹅场的心已经按捺不住了),这个问题还是建立最小堆比较好一些. 先拿10000个数建堆, ...
- redis删除所有key
flushdb 删除当前数据库的所有keyflushall 删除所有数据库的所有keydbsize 返回当前数据库的key的数量
- CSS基础:替换元素和非替换元素
简介 根据 "外在盒子" 是内联还是块级我们可以把元素分为内联元素和块级元素,而根据是否具有可替换内容,我们也可以把元素分为替换元素和非替换元素.这种通过修改某个属性值,例如 &l ...
- Xshell 5 配置上传下载命令
可以在官网https://www.netsarang.com/products/main.html 下载Xshell, 目前最新的版本已经到Xshell 6了 本人记录下安装的目录截图: 安装命令: ...
- Ubuntu硬盘空间清理
1.删除多余的安装下载文件: sudo aptitude autoclean sudo aptitude clean 2.删除多余的内核版本: sudo apt-get autoremove 4.去/ ...