02-线性结构4 Pop Sequence (25分)
02-线性结构4 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
实现一个栈的数据结构,用数组和一个下标来模拟。判断是否会出现堆栈爆满的情况。
#include <stdio.h>
#define MAXVAL 1000
static int sp = 0;
static int val[MAXVAL];
int push(int i, int M)
{
int ret = 0;
if (sp < M) {
val[sp++] = i;
}
else {
ret = -1;
}
return ret;
}
int pop(void)
{
return sp > 0 ? val[--sp] : 0;
}
int get_top(void)
{
return sp > 0 ? val[sp - 1] : 0;
}
int main(int argc, char const *argv[])
{
int M, N, K;
scanf("%d %d %d", &M, &N, &K);
while (K--) {
sp = 0;
int line[N];
for (int i = 0; i < N; i++) {
scanf("%d", &line[i]);
}
int index = 0, x = 2;
push(1, M);
int ok = 1;
while (index < sizeof(line) / sizeof(line[0])) {
int top = get_top();
if (top == line[index]) {
pop();
index++;
}
else if (push(x++, M) < 0) {
ok = 0;
break;
}
}
if (ok) {
printf("YES\n");
}
else {
printf("NO\n");
}
}
return 0;
}
02-线性结构4 Pop Sequence (25分)的更多相关文章
- PTA 02-线性结构4 Pop Sequence (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence (25分) Given a stack wh ...
- 02-线性结构4 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 ...
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 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 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- 线性结构4 Pop Sequence
02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...
- 数据结构练习 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 ...
- 浙大数据结构课后习题 练习二 7-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 and p ...
- 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)
题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...
随机推荐
- 第一个Django应用 - 第七部分:自定义admin
Django的admin站点是自动生成的.高度可定制的,它是Django相较其它Web框架独有的内容,广受欢迎.如果你觉得它不够美观,还有第三方美化版xadmin.请一定不要忽略它,相信我,它值得拥有 ...
- Logstash:如何处理 Logstash pipeline 错误信息
转载自:https://elasticstack.blog.csdn.net/article/details/114290663 在我们使用 Logstash 的时候经常会出现一些错误.比如当我们使用 ...
- 复现CVE-2022-10270(向日葵远程代码执行漏洞)
警告 请勿使用本文提到的内容违反法律.本文不提供任何担保. 漏洞描述 向日葵是一款免费的,集远程控制电脑手机.远程桌面连接.远程开机.远程管理.支持内网穿透的一体化远程控制管理工具软件.CNVD披露了 ...
- NAT模式下的虚拟机连接主机网络
基于NAT模式的VMware虚拟机(Linux CentOS 7)连接主机(Windows 11)网络 一.什么是NAT模式 虚拟机连接主机网络的三种方式: Bridged(桥接) NAT(网络地址转 ...
- PAT (Basic Level) Practice 1018 锤子剪刀布 分数 20
大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入 ...
- MybatisPlus生成主键策略方法
MybatisPlus生成主键策略方法 全局id生成策略[因为是全局id所以不推荐] SpringBoot集成Mybatis-Plus 在yaml配置文件中添加MP配置 mybatis-plus: g ...
- sql面试50题------(1-10)
文章目录 1.查询课程编号'01'比课程编号'02'成绩高的所有学生学号 2.查询平均成绩大于60分得学生的学号和平均成绩 3.查询所有学生的学号,姓名,选课数,总成绩 4.查询姓"猴&qu ...
- JUI(6)线程池
文章目录 1.SynchronousQueue 2.线程池(重点) 2.1 使用单例 2.2.使用固定大小的线程 2.3.缓存线程池 2.4 七大参数 1.SynchronousQueue packa ...
- 齐博x1服务器性能太差,调整系统升级每次校验的文件数
系统升级需要校验本地的文件是否被修改过,系统默认每次检验1千个文件,一般来说需要分四到五页来处理,如下图所示. 如果你的服务器性能太差的话,就需要手工把数值调小.把下面的代码复制出来.进入后台数据库管 ...
- 抛砖系列之redis监控命令
前言 redis是一款非常流行的kv数据库,以高性能著称,其高吞吐.低延迟等特性让广大开发者趋之若鹜,每每看到别人发出的redis故障报告都让我产生一种居安思危,以史为鉴的危机感,恰逢今年十一西安烟雨 ...