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按照顺序随机入栈(入栈时机随机,未知 ...
随机推荐
- Kubernetes 中部署 MySQL 集群
文章转载自:https://www.cnblogs.com/ludongguoa/p/15319861.html 一般情况下 Kubernetes 可以通过 ReplicaSet 以一个 Pod 模板 ...
- 修改因python是3版本导致的yum问题
问题原因: 系统自带的python出来的是2版本,但是因为某些原因,比如安装使用ElartAlert,导致执行python出来的是3版本. 此时执行yum相关的命令,会报错,具体错误信息如下: [ro ...
- windows下利用_popen,_wpoen创建管道进行系统命令输出数据
转载: https://blog.csdn.net/greless/article/details/72383762 参考: http://www.linuxidc.com/Linux/2011-04 ...
- 后端框架的学习----mybatis框架(3、配置解析)
3.配置解析 1.核心配置文件 2.环境配置(environment) 3.属性(properties) 可以通过properties属性来实现引用配置文件 这些属性可以在外部进行配置,并可以进行动态 ...
- LeetCode------找到所有数组中消失的数字(6)【数组】
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array 1.题目 找到 ...
- 学生管理系统(C语言简单实现)
仅供借鉴.仅供借鉴.仅供借鉴(整理了一下大一C语言每个章节的练习题.没得题目.只有程序了) 文章目录 1 .实训名称 2.实训目的及要求 3. 源码 4.实验小结 1 .实训名称 实训12:文件 2. ...
- 忘记了99乘法表啥样的了,python打印下看看
for i in range(1,10): for j in range(1, i+1): if i == j: print(j, "x", i, "=", i ...
- c++算法竞赛常用板子集合(持续更新)
前言 本文主要包含算法竞赛一些常用的板子,码风可能不是太好,还请见谅. 后续会继续补充没有的板子.当然我太菜了有些可能写不出来T^T 稍微有些分类但不多,原谅我QwQ 建议 Ctrl + F 以快速查 ...
- 【MySQL】03_数据类型
MySQL 中的数据类型 类型 类型举例 整数类型 TINYINT.SMALLINT.MEDIUMINT.INT(或INTEGER).BIGINT 浮点类型 FLOAT.DOUBLE 定点数类型 DE ...
- 在FreeSQL中实现「触发器」和软删除功能
前言 最近做新项目,技术栈 AspNetCore + FreeSQL 这个ORM真的好用,文档也很完善,这里记录一下两个有关「触发器」的功能实现 修改实体时记录更新时间 模型代码 我的模型都是基于这个 ...