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按照顺序随机入栈(入栈时机随机,未知 ...
随机推荐
- redhat替换yum源时redhat.repo无法删除或禁用的问题
rhel7.3系统,在替换自带的repo源时发现无论是将redhat.repo重命名还是删除,在执行yum命令后总是自动又生成redhat.repo得问题,导致替换的CentOS-Base.repo, ...
- 阿里云服务器部署Web环境
一.配置阿里云服务器 进入阿里云官方网站(https://www.aliyun.com/). 初次使用的话使用支付宝快速注册账户,并进行个人实名认证. 点击试用中心. 选择第二个,云服务器2核4G. ...
- centos下如何获取某个命令的源代码?
以ls命令为例,其它命令类似: 1.利用which命令获取命令可执行文件的位置: [root@228 /]# which ls alias ls='ls --color=auto' /usr/bin/ ...
- 把train数据集生成txt(test同理)
import cv2 import numpy as np import os import sys import pickle data_dir = os.path.join("./&qu ...
- 2022年最新最详细的tomcat安装教程和常见问的解决
文章目录 1.官网直接下载 1.1.jdk的版本和tomcat版本应该相对应或者兼容 1.2. 在官网找对应的tomcat版本进行下载 1.3 .根据电脑版本下载64-bit windows zip( ...
- 获取cpu的核数
//获取cpu的核数 System.out.println(Runtime.getRuntime().availableProcessors());
- 题解 UVA10285 最长的滑雪路径 Longest Run on a Snowboard
Solution 双倍经验 就是记搜嘛. 搞一个二维数组记录一下当前的最长滑雪路径,其他和普通 dfs 没什么两样. 向 \(4\) 个方向搜索,如果高度符合就 \(+1\) . 多测要注意数组初始化 ...
- N32G4系列——复用功能重映射(USART为例)
开发测试环境:SDK,N32G455x系列芯片 在国民MCU中G系列IO口有第二复用功能,这时需要用到重映射功能. 一.系列芯片手册定义 1.1.芯片IO口默认功能查看 如图,在该系列芯片的数据手册中 ...
- JS逆向实战1——某省阳光采购服务平台
分析 其实这个网站基本没有用到过什么逆向,就是简单的图片base64加密 然后把连接变成2进制存成文件 然后用ocr去识别即可 !! 注意 在获取图片连接 和对列表页发起请求时一定要用一个请求,也就是 ...
- Android10 dex2oat实践
最近看到一篇博客:Android性能优化之Android 10+ dex2oat实践,对这个优化很感兴趣,打算研究研究能否接入到项目中.不过该博客只讲述了思路,没有给完整源码.本项目参考该博客的思路, ...