02-线性结构4 Pop Sequence

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
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct SNode{
 4     int *data;
 5     int top;
 6     int maxsize;
 7 };
 8 typedef struct SNode *stack;
 9
10 //初始化堆栈函数
11 stack createstack(int maxsize){
12     stack S = (stack)malloc(sizeof(struct SNode));
13     S->data = (int*)malloc(sizeof(int)*maxsize);
14     S->top = -1;
15     S->maxsize = maxsize;
16     return S;
17 }
18
19 //判断堆栈是否为空函数
20 bool Isempty(stack S){
21     return (S->top == -1);
22 }
23
24 //入栈函数
25 void push(stack S, int temp) {
26     S->data[++(S->top)] = temp;
27 }
28
29 //出栈函数
30 void pop(stack S){
31     if(S->top==-1) return;
32     else {
33         S->top--;
34     }
35 }
36
37 int main( ){
38     int M, N, K;
39     scanf("%d%d%d", &M, &N, &K);
40     stack S = createstack(M);
41     while(K--){
42         bool flag = true;
43         int temp = 1, num;
44         for(int i=0; i<N; i++){                //循环读入push sequence的每个数
45             scanf("%d", &num);
46             while(S->top<S->maxsize && flag)          //当堆栈不满,且前面的元素均是可能的出栈序列时
47             {
48                 if(Isempty(S) || S->data[S->top]!=num)   //若堆栈空,或栈顶元素不等于num
49                 {
50                     push(S, temp++);                 //将temp按升序压入堆栈
51                 }
52                 else if(S->data[S->top]==num)        //直到栈顶元素等于num
53                 {
54                     pop(S);                          //将元素出栈
55                     break;
56                 }
57             }
58             if(S->top>=S->maxsize)       //如果temp一直压到堆栈满,仍未找到符合num的出栈元素
59             {
60                 flag = false;            //说明该序列不是可能的出栈序列,将flag设为false
61             }
62         }
63         if(flag) printf("YES\n");
64         else printf("NO\n");
65         while(!Isempty(S))    //每个序列判断完,将堆栈清空
66         {
67             pop(S);
68         }
69     }
70     return 0;
71 }

原文地址:https://www.cnblogs.com/shin0324/p/9797085.html

时间: 2024-08-28 17:37:37

02-线性结构4 Pop Sequence的相关文章

线性结构4 Pop Sequence

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, ..., Nand pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example,

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

数据结构练习 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 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 ca

数据结构 02-线性结构4 Pop Sequence

刚开始拿到这道题的时候错误的做法: 1 //相邻之间的数小于等于M 2 //首先弹出来的数小于等于M 3 #include<stdio.h> 4 #include<math.h> 5 #define MAXN = 1000000; 6 int M,N,K; 7 int main(){ 8 int pre,now,i,j,flag=0; 9 scanf("%d %d %d",&M,&N,&K); 10 for(i=0;i<K;i++)

PAT线性结构_一元多项式求导、按给定步长反转链表、出栈序列存在性判断

02-线性结构1. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数.数字间以空格分隔,但结尾不能有多余空格.注意“零多项式”的指数和系数都是0,但是表示为“0 0”. 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 最简单的方式是用

13. C#数据结构与算法 -- 线性结构

本文中,我们讨论了三个部分的内容: 什么是线性结构,线性结构有哪些特点 . 详细介绍了一个最简单线性结构顺序表,并且通过源代码进行一些的分析. 最后还举了一个例子,让我们更好的理解顺序表. 第一部分:什么是线性结构,线性结构有哪些特点 什么是线性结构,线性结构是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这 种一对一的关系指的是数据元素之间的位置关系,即: (1)除第一个位置的数据元素外,其它数据元素

数据结构第二讲:线性结构

参考:浙大数据结构(陈越.何钦铭)课件 1.线性表及其实现 有一个很好的问题可以方便的说明引入链表的好处,那就是一元多项式:f(x) = a0 + a1x + an-1xn-1 + anxn 的表示及运算(两个多项式相加/相减/相乘等),显然我们可以利用数组来解决这个问题,两个多项式相加就是两个数组对应分量相加.但是这引入了一个很严重的问题,如何表示多项式x + 3x2000呢?开这么大的数组明显会造成空间浪费. 解决上面遗留的问题的一个方法是用结构数组按指数大小有序存储,每一个数组元素维护两个

数据结构和算法-数据结构-线性结构-栈和队列

 ################################################## """ 三.线性结构 (1)栈 1.定义:栈是一个数据集合,可以理解为只能在一端进行插入或者删除操作的列表. 2.栈的特点:后进先出(last-in,first-out),简称LTFO表 这种数据结构的特点: 就是像是杯子或者是弹夹,电梯, 存储的时候从底部开始,读取的时候从顶部开始,具备这种特点就是栈 就是后进先出, 存储的时候就可以从顺序表或者链表就可以实现, 只让从一

算法--线性结构

一.数据结构 什么是数据结构:数据与数据之间的关系. 数据的存储结构:顺序存储(ArrayList).链式存储(LinkList). 数据的逻辑结构:集合结构.线性结构.树形结构.图形结构. 二.算法 算法:解决问题的方法. 算法的特性:输入.输出.有穷.确定性.可行性. 算法的基本要求:正确性.可读性.健壮性.时间复杂度.空间复杂度. 三.线性结构 1.数组 普通数组:类似Java基本类型数组 对象数组:类似ArrayList在对象里面定义一个数组类型的成员变量. 数组中的算法:线性查找和二分