线性结构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, 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<iostream>
 2 #include<stack>
 3 #include<vector>
 4 #include<algorithm>
 5 using namespace std;
 6 int M,N,K;
 7 int check(vector<int> &vi){
 8     int m=0,n=0,cap=M+1;
 9     stack<int> sta;
10     sta.push(0);
11     while(n<N){
12         while(sta.size()<cap&&vi[n]>sta.top())
13             sta.push(++m);
14         if(sta.top()==vi[n++]) sta.pop();
15             else return 0;
16         }
17     return 1;
18 }
19 int main()
20 {
21     cin>>M>>N>>K;
22     vector<int> vi(N,0);
23     for(int j=0;j<K;j++){
24         for(int i=0;i<N;i++){
25         int n;
26         cin>>n;
27         vi[i]=n;}
28         if(check(vi))
29         cout<<"YES"<<endl;
30         else
31         cout<<"NO"<<endl;
32         vi.clear();
33     }
34     return 0;
35  }

 

 

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

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

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

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在对象里面定义一个数组类型的成员变量. 数组中的算法:线性查找和二分