PAT 甲级 1051 Pop Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944
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 <bits/stdc++.h>
using namespace std; int N, M, K;
vector<int> v; int main() {
scanf("%d%d%d", &M, &N, &K);
while(K --) {
bool flag = false;
v.resize(N + 1);
int cnt = 1;
for(int i = 1; i <= N; i ++)
scanf("%d", &v[i]); stack<int> s;
for(int i = 1; i <= N; i ++) {
s.push(i);
if(s.size() > M) break;
while(!s.empty() && s.top() == v[cnt]) {
s.pop();
cnt ++;
}
} if(cnt == N + 1) flag = true;
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
感觉年就这么结束了诶
PAT 甲级 1051 Pop Sequence的更多相关文章
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
- 【PAT】1051 Pop Sequence (25)(25 分)
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- 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 ...
- PAT Advanced 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 ...
- 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 ...
- PAT 1051 Pop Sequence[栈][难]
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- PAT 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- 1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...
- 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 ...
随机推荐
- Kafka学习之路 (一)Kafka的简介
一.简介 1.1 概述 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/ng ...
- SpringMVC之ajax+select下拉框交互常用方式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- jpa实例
ORM框架新的JPA ORM规范:1)JPA为POJO提供持久化标准规范.2)JPA的使用:Hibernate与TopLink以及OpenJpa都提供了JPA的实现.3)JPA主要技术: A.ORM ...
- CentOS7服务器配置网络
Centos7最小化安装 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-enp5s0f0编辑如下:TYPE=Ethernet ...
- Android Fragment(二)
废话:在上一篇的博客中我们给出了Fragment的简单介绍,这一片博客给大家介绍一下Fragment到底该怎样用.主要都用在哪方面等等. 需求:现有一个界面,要求,竖屏时界面的背景颜色为红色,横屏时界 ...
- MVC的BundleConfig应用
1.MVC可以通过BundleConfig类来配置css和js的统一引用,分别通过StyleBundle和ScriptBundle来创建. 2.可以在母版页中统一加载设置在BundleConfig.c ...
- odoo权限
ir.model.access.csv文件这里注意,用户和经理的写法 id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create, ...
- R语言--输入输出
基本输入输出 输入: readline, edit, fix 输出: print, cat 输出重定向 sink #基本输入输出 x=readline('请输入:') #读取输入,一行为一个字符串 x ...
- 20155331《网络对抗》Exp5 MSF基础应用
20155331<网络对抗>Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode 答:exploit就是漏洞利用.exploit就是利用 ...
- 汇编 REPE/REPZ 指令,CMPSB指令
知识点: REPE/REPZ 指令 CMPSB 指令 一.CMPSB //cmp //sub //SCASB//scasw//scasd cmp byte ptr [edi],al //对标志位的 ...