题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/665

5-3 Pop Sequence   (25分)

Given a stack which can keep MM numbers at most. Push NN numbers in the order of 1, 2, 3, ..., NN 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 MM is 5 and NN 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): MM (the maximum capacity of the stack), NN (the length of push sequence), and KK (the number of pop sequences to be checked). Then KK lines follow, each contains a pop sequence of NN 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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 15:55 答案正确 25 5-3 gcc 3 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 15/15 2 1
测试点2 答案正确 3/3 2 1
测试点3 答案正确 2/2 2 1
测试点4 答案正确 2/2 3 1
测试点5 答案正确 1/1 2 1
测试点6 答案正确 2/2 1 1 */
#include<stdio.h>
#define MAXLEN 1000 /*下面这段代码只能拿18/25分
int main()
{
int i,j,m,n,k,last,flag,temp;
scanf("%d %d %d",&m,&n,&k);
for (i=0;i<k;i++){
flag=1;
last=0;
for(j=0;j<n;j++){
scanf("%d",&temp);
if (temp-last>m) flag=0;
last=temp;
}
if(flag==0) printf("NO\n");
else printf("YES\n"); }
}
*/ struct stack{
int data[MAXLEN];
int top;
int max;
}; struct stack workstack; int GetTop(struct stack stc)
{
if(stc.top>=0) return stc.data[stc.top];
else return 0;
} int Push(struct stack *stc,int item)
{
if (stc->top==MAXLEN-1) return 0;
else {
stc->data[++(stc->top)]=item;
// printf("--PUSH %d,%d\n",stc->data[(stc->top)],stc->top);//test
return 1;
}
} int Pop(struct stack *stc)
{
if(stc->top<0) return 0;
else{
// printf("--POP %d,%d\n",stc->data[(stc->top)],stc->top);//test
return stc->data[(stc->top)--];
}
} int main()
{
int i,j,m,n,k,numforpush,errorflag,temp;
scanf("%d %d %d",&m,&n,&k);
workstack.max=m;
for (i=0;i<k;i++){
errorflag=0;
numforpush=1;
workstack.top=-1;
for(j=0;j<n;j++){
scanf("%d",&temp);
if (errorflag==1) continue;
// printf("--GET %d\n",temp);//test
while(workstack.top<workstack.max-1 && numforpush<=n && GetTop(workstack)!=temp){
// printf("--IN WHILE workstack.top=%d,max=%d,numforpush=%d\n",workstack.top,workstack.max,numforpush);//test
Push(&workstack,numforpush);
numforpush++;
} if(GetTop(workstack)==temp){
Pop(&workstack);
}
else{
errorflag=1;
} }
if(errorflag==1)
printf("NO");
else
printf("YES");
if(i!=k-1) putchar('\n');
} }

PTA 02-线性结构4 Pop Sequence (25分)的更多相关文章

  1. 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 ...

  2. pat02-线性结构4. Pop Sequence (25)

    02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  3. 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 ...

  4. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  5. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

  6. 数据结构练习 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. 浙大数据结构课后习题 练习二 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 ...

  8. 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 ...

  9. [刷题] PTA 02-线性结构4 Pop Sequence

    模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...

随机推荐

  1. H5页面快速搭建之高级字体应用实践

    原文出处: 淘宝前端团队(FED)- 龙驭 背景 最近在开发一个 H5 活动页快速搭建平台,可以通过拖拽编辑图片,文字等元素组件,快速搭建出一个移动端的活动页面,基本交互和成品效果类似 PPT 软件. ...

  2. MySQL GTID复制

    什么是GTID 什么是GTID呢, 简而言之,就是全局事务ID(global transaction identifier ),最初由google实现,官方MySQL在5.6才加入该功能.GTID是事 ...

  3. 关于spring mvc 和struts2的描述与对比

    链接:https://www.nowcoder.com/questionTerminal/cf803beb7e3346caa636e4eaa3a8c2e9来源:牛客网 ---------------- ...

  4. 基于ABP的Easyui admin framework正式开放源代码

    下载&反馈:http://www.webplus.org.cn v1.0 (2016/9/21) EF6+MVC5+API2+Easyui1.4.2开发 后台管理不使用iframe,全ajax ...

  5. ubuntu下php-fpm多实例运行配置

    php-fpm服务一般情况下我们只会配置一个php-fpm了,如果我们碰到要实现多实例php-fpm服务要如何来配置呢,下面一起来看看吧. 这里是在LNMP环境的基础上配置多实例的过程.因为我在使用的 ...

  6. Redis学习笔记(三)列表进阶

    RPOPLPUSH source destination(弹出source列表最右端的元素,并推入destination的最左端,同时返回这个元素) BRPOPLPUSH source destina ...

  7. phpstorm 格式化代码

    MAC 安装phpcs.phpcbf composer global require 'squizlabs/php_codesniffer=*' Changed current directory t ...

  8. oracle补丁类型

    名称 说明 Release ¤ 标准产品发布.如Oracle Database 10g Release 2的第一个发行版本为10.2.0.1,可以在OTN.edelivery等站点上公开下载 Patc ...

  9. vux安装

    1. 在项目里安装vux cnpm install vux --save 2. 安装vux-loader cnpm install vux-loader --save-dev 3. 安装less-lo ...

  10. Swift中Singleton的实现

    一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...