浙大数据结构第二周之02-线性结构4 Pop Sequence

news/2025/2/13 3:17:48/

题目详情:

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

简单翻译:

本题就是告诉你一个最大空间为m的堆栈,往这个堆栈里面依次加入1~n的数字,再给n个测试顺序,判断每个顺序能否实现

主要思路:

建立一个辅助栈,想这个辅助栈里压入1~n

给出的顺序无法实现有两种情况,一种是栈的空间不够大,比如栈的空间是5,给7 6 5 4 3 2 1就不能;一种是顺序问题

外层循环遍历从1~n,如果栈没有满,就压入栈

内层循环判断栈顶元素和cur指针当前所指元素是否相同,如果相同,就弹出栈顶元素,并且cur指针指向下一位

最后如果反应循环过程中栈没满的flag为true和辅助栈最后为空,说明该序列合法

原理……还没想好

第一次写错误:

判断栈有没有满不能用MAX_SIZE,而要定义一个全局变量读入题干所给的M

代码实现:

#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 1005
#define EMPTY -1
/*实现栈的数据结构*/
typedef struct StackNode Stack;
struct StackNode {int Data[MAX_SIZE];int Top;
};/*栈的初始化*/
void InitStack(Stack* stackName) {(*stackName).Top = -1;return;
}
/*检测栈有没有满,返回false是没满,返回true是满了*/
int CAPACITY;
bool IsFull(Stack* stackName) {bool ret = false;if((*stackName).Top == CAPACITY - 1) ret = true;return ret;
}/*检测栈是不是为空,返回true就是空,返回false就是不空*/
bool IsEmpyt(Stack* stackName) {bool ret = false;if((*stackName).Top == EMPTY) ret = true;return ret;
}/*向栈里加入元素*/
void Push(Stack* stackName, int data) {if(IsFull(stackName)) {return;}int position = (*stackName).Top;(*stackName).Data[++position] = data;(*stackName).Top = position;return;
}/*从栈里弹出元素*/
void Pop(Stack* stackName) {if(IsEmpyt(stackName)) {return;}int topPosition = (*stackName).Top;int topData = (*stackName).Data[topPosition--];(*stackName).Top = topPosition;
}/*访问栈顶元素*/
int Top(Stack* stackName) {if(IsEmpyt(stackName)) {return EMPTY;}return (*stackName).Data[(*stackName).Top];
}
/*读入待判断数据,返回判断结果*/
bool Judge(const int* num, int dataLength) {Stack s;InitStack(&s);int cur = 0;bool flag = true;for(int i = 1; i <= dataLength; i++) {if(!IsFull(&s)) {Push(&s, i);}else {flag = false;break;}while(!IsEmpyt(&s) && num[cur] == Top(&s)) {Pop(&s);cur++;}}if(flag == true && IsEmpyt(&s)) {return true;}else return false;
}
int main() {int N, K;    //M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). scanf("%d %d %d", &CAPACITY, &N, &K);int num[N];for(int i = 0; i < K; i++) {for(int j = 0; j < N; j++) {scanf("%d", &(num[j]));}if(Judge(num, N)) {printf("YES\n");}else {printf("NO\n"); }}return 0;
}


http://www.ppmy.cn/news/836402.html

相关文章

常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)

一. shell基础案例 1、第一个案例&#xff1a;helloworld #!/bin/bashfunction example {echo "Hello world!" } example2、打印运行的python进程 #!/bin/sh pidlistps -aux | grep python | awk {print $2} echo $pidlist3、获取并打印参数 #!/bin/bash echo &q…

统计连续字符-2022年全国青少年信息素养大赛Python国赛第7题

[导读]&#xff1a;超平老师计划推出《全国青少年信息素养大赛Python编程真题解析》50讲&#xff0c;这是超平老师解读Python编程挑战赛真题系列的第9讲。 全国青少年信息素养大赛&#xff08;原全国青少年电子信息智能创新大赛&#xff09;是“世界机器人大会青少年机器人设计…

实现Android底层驱动开发并裁剪定制Android操作系统

毕业论文 题 目 实现Android底层驱动开发并裁剪定制Android操作系统 学 院 电子信息与电气工程学院 姓 名 牛xxx民 专 业 电子信息科学与技术 学 号 2012xxxxxxx …

stm32 001 - hello world(附带可运行源码)

文章目录 初衷前提准备硬件环境软件环境硬件连接 Hello world应用在STM32CubeIDE中创建新工程配置引脚修改代码运行及调试 源码 初衷 我是做Android开发的&#xff0c;因此本文许多地方会比较繁琐&#xff0c;各位做单片机开发的轻喷呀 有幸接触到stm32这个平台&#xff0c;之…

仿标准库,对HAL库的补充代码

前言&#xff1a; ST官方从2017年下半年开始就不再维护升级标准库&#xff0c;转而推广HAL库。到2019年&#xff0c;HAL库仍不够成熟&#xff0c;其原因有以下&#xff1a; 1. HAL库的配套指导文档&#xff0c;特别是中文的使用手册文档欠缺得很厉害&#xff0c;除了野火在…

音视频中的基本概念

文件格式 操作系统中的文件名都有后缀&#xff0c;即扩展名&#xff0c;例如1.doc&#xff0c;2.jpg&#xff0c;3.avi等。设置扩展名的目的是让系统中的应用程序来识别并关联这些文件&#xff0c;让相应的文件由相应的应用程序打开。常见的文件格式如1.avi&#xff0c;2.mpg&…

移动数据库发展现状报告

嵌入式移动数据库发展现状 ——嵌入式系统课程调研报告 一&#xff0e;嵌入式移动数据库系统的需求与特点 随着以智能手机为代表的&#xff0c;包括车载娱乐系统、智能家居、可穿戴设备在内的现代 嵌入式系统在硬件性能上的飞速发展&#xff0c;其对于数据库系统的支持能力…

Android平台安全模型(The Android Platform Security Model)

版权声明&#xff1a;本文为CSDN博主「ashimida」翻译的文章&#xff0c;转载请附上出处链接和本声明。 本文链接&#xff1a;https://blog.csdn.net/lidan113lidan/article/details/119429366 此文翻译自<The Android Platform Security Model>(2019),译者水平有限,如有…