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 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<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main(){
int N, M, K, temp;
stack<int> stk;
scanf("%d%d%d", &M, &N, &K);
for(int i = ; i < K; i++){
int index = , find = ;
for(int j = ; j < N; j++){
scanf("%d", &temp);
if(stk.empty()){
stk.push(index++);
}
if(stk.top() < temp){
while(stk.top() < temp && stk.size() < M){
stk.push(index++);
}
if(stk.top() < temp)
find = ;
}
if(stk.top() > temp){
find = ;
continue;
}
if(stk.top() == temp){
stk.pop();
continue;
}
}
if(find == )
printf("NO\n");
else printf("YES\n");
while(!stk.empty()){
stk.pop();
}
}
cin >> N;
return ;
}
总结:
1、栈的模拟题。注意栈的大小有限制。
2、注意每次使用完毕后清空stack。
A1051. Pop Sequence的更多相关文章
- PAT甲级——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 ...
- 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 ...
- PAT1051:Pop Sequence
1051. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
随机推荐
- Helper
//检测端口是否使用 public static bool VerifyListenerPort(int port) { bool inUse = false; System.Net.NetworkI ...
- LInux下设置账号有效时间 以及 修改用户名(同时修改用户组名和家目录)
在linux系统中,默认创建的用户的有效期限都是永久的,但有时候,我们需要对某些用户的有效期限做个限定!比如:公司给客户开的ftp账号,用于客户下载新闻稿件的.这个账号是有时间限制的,因为是付费的.合 ...
- 关于QQ的NABCD模型
组名:思甜雅 关于QQ的NABCD模型 N--Need 随着电脑的普及,人们需要在网络上进行交流,现有的交流工具不够完善,不能够全部满足人们对于交流沟通中的需要. 初步了解人们的需求,人们需要一款可以 ...
- 第三个spring冲刺第8天
今天,我们忙于完成精美的背景,还有难度的具体设置,如何达到最理想化,为此我们今天主要是做了开会讨论,但还没有完全确定好结论,明天就应该能做出结论,然后修改后台的难度设置了.
- Install Kernel 3.10 on CentOS 6.5
http://bicofino.io/2014/10/25/install-kernel-3-dot-10-on-centos-6-dot-5/ https://gree2.github.io/lin ...
- CRM 数据查重
2.8 小工具 · 纷享销客产品手册https://www.fxiaoke.com/mob/guide/crmdoc/src/2-8%E5%B0%8F%E5%B7%A5%E5%85%B7.html C ...
- CentOS Mininal 安装VMtools的方法
1. 下载安装CentOS75 的mininal版本 2. 安装完成之后挂在vmtools. 虚拟机管理,安装vmtools即可 3. ssh登录虚拟机. cd /dev 进入到设备系统 mount ...
- FuelPHP 系列(五) ------ Security 防御
项目中难免会有 form 提交,对用户输入的所有信息进行过滤,可以避免 XSS 攻击,防止 SQL 注入. 一.设置配置信息 首先在 config.php 文件中,对 security 相关信息进行设 ...
- centos安装图形化界面
用下面命令查看查看是哪个模式 systemctl get-default 开机启动图形界面 systemctl set-default graphical.target(图形界面模式) reboot( ...
- link & auto cards
link & auto cards a link to card link https://docs.embed.ly/docs/cards DD WX <blockquote clas ...