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按照顺序随机入栈(入栈时机随机,未知 ...
随机推荐
- Traefik2.3.x 使用大全(更新版)
文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247488793&idx=1&sn=bb2b0ad1 ...
- MongoDB $type 操作符
描述 $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果. MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1 String 2 Object 3 ...
- Elasticsearch:使用 GeoIP 丰富来自内部专用 IP 地址
转载自:https://blog.csdn.net/UbuntuTouch/article/details/108614271 对于公共 IP,可以创建表来指定 IP 属于哪个城市的特定范围.但是,互 ...
- C#-7 结构和枚举
一 什么是结构 结构是程序员定义的数据类型,有数据成员和函数成员,和类非常类似. 类是引用类型,而结构是值类型: 结构和类一样可以实现接口: 结构是隐式密封的,不能被派生: 结构类型的变量不能为nul ...
- 关于在Linux上安装aws cli版本1的记录
AWS Command Line Interface (AWS CLI) 是一种开源工具,让您能够在命令行 Shell 中使用命令与 AWS 服务进行交互. 因公司项目要求,要在Linux服务器安装a ...
- Java中的多线程的创建方式
首先理清几个基本概念: 程序:为完成特定任务,用某种语言编写的一组指令的集合.即一段静态的代码(还没运行起来) 进程:是程序的一次执行过程,也就是说程序运行起来了,加载到了内存中,并占用了cpu的资源 ...
- python基础-较复杂数据类型预览
1.初识列表 列表就是队列: 列表是一种有序的,且内容可重复的数据类型: 用list代表列表,也可以用list()定义一个列表,同时定义列表可以直接使用 [ ]: python中列表是 ...
- 基于纯前端类Excel表格控件实现在线损益表应用
财务报表也称对外会计报表,是会计主体对外提供的反映企业或预算单位一定时期资金.利润状况的会计报表,由资产负债表.损益表.现金流量表或财务状况变动表.附表和附注构成.财务报表是财务报告的主要部分,不包括 ...
- Collection接口中的方法的使用
add(Object e):将元素e添加到集合coll中size():获取添加的元素的个数addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中clear():清 ...
- [Thread] Synchronized
1.Monitor对象 Monitor对象被称为管程或者监视器锁. 在Java中,每一个对象实例都会关联一个Monitor对象. 这个Monitor对象既可以与对象一起创建销毁,也可以在线程试图获取对 ...