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<iostream>
#include<stack>
using namespace std; const int maxn = ; int check(int *a,int n,int m); int main()
{
int arr[maxn] = {};
int n,m,k;
scanf("%d%d%d",&m,&n,&k); for (int i = ; i < k; i++)
{
for (int j = ; j <= n; j++)
{
scanf("%d",&arr[j]);
}
if(check(arr,n,m))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
} int check(int *a,int n,int m)
{
int iRet = -;
stack<int> s;
int current = ;
bool flag = true;
for (int i = ; i <= n; i++)
{
s.push(i);
if (s.size() > m)
{
flag = false;
break;
}
while (!s.empty() && a[current] == s.top())
{
current++;
s.pop();
}
} if (flag && s.empty())
{
iRet = ;
}
else
{
iRet = ;
} return iRet;
}
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 ...
- 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按照顺序随机入栈(入栈时机随机,未知 ...
随机推荐
- .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
原文:.NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖 我们有多种工具可以将程序集合并成为一个.打包成一个程序集可以避免分发程序的时候带上一堆依赖而出问题. ILMerge 可以用来 ...
- Delphi中AssignFile函数
procedure TForm1.SaveLog(sFlag:string;MSG:string);var QF1:Textfile; ----声明文本文件类型 Qfiletmp,sP ...
- Java——简单实现学生管理系统
import java.io.*;import java.util.ArrayList;import java.util.Scanner;class MyObjectOutputStream exte ...
- Tensorflow在python3.7版本的运行
安装tensorflow pip install tensorflow==1.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple 可以在命令行 或者在py ...
- Matlab外观模式
外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口.本文以计算机为例,用Matlab代码实现外观模式.计算机包括CPU.内存以及硬盘等这些部件.用户 ...
- MyCat - 数据库中间插件
什么是MyCat 是目前最流行的分布式数据库中间插件 为什么使用MyCat 如今随着互联网的发展,数据的量级也是撑指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经 ...
- Swiper4的基本使用
基本介绍: 中文文档地址:https://www.swiper.com.cn/ 它是一个开源,免费,强大的触摸滑动插件. 它是用纯Javascript打造的滑动特效插件,既可用于PC端,也可用于移动端 ...
- Python学习笔记-数字,列表,元祖,切片,循环
数字 1,加减乘除:+,-,*,/ 2,平方:** 3,立方:**3 4,字符串转换:str(数字) 5,浮点数:带小数点 0.2 Python编程建议 import this >>&g ...
- nginx服务器除了更目录可以访问,其他都出现404
配置如下: listen 80; server_name www.hongtaofei.com; location / { root /home/www/shop/public; index inde ...
- mysql表的创建、查看、修改、删除
一.创建表 创建表前先使用use 数据库名进入某一个数据库,创建表语句的格式如下: create table 表名称 ( 列名1 列的数据类型 [约束], 列名2 列的数据类型 [约束], 列名2 列 ...