1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051
题目:
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
这道题我参阅了 http://blog.csdn.net/whzyb1991/article/details/46663867 这篇博文的思路,稍有改动,并且我是用链式存储堆栈,而博文的作者是用顺序存储的方式,读者可以对比两种
方式的优缺点。
这道题并不是有多复杂,关键在于想出it is not a possible pop sequence of the stack的条件!我是苦于没有思路,才参阅了博主的文章。共勉!
我的代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define Max 1000
typedef struct SNode{
int Data;
struct SNode *Next;
}Stack; Stack *CreateStack()
{
Stack *Ptrs=(Stack*)malloc(sizeof(struct SNode));
Ptrs->Next=NULL;
return Ptrs;
} bool IsEmpty(Stack *Ptrs)
{
return(Ptrs->Next==NULL);
} void Push(Stack *Ptrs,int X)
{
Stack *S;
S=(Stack *)malloc(sizeof(struct SNode));
S->Data=X;
S->Next=Ptrs->Next;
Ptrs->Next=S;
} void Pop(Stack *Ptrs)
{
if(IsEmpty(Ptrs)) return;
Stack *FirstCell;
FirstCell=Ptrs->Next;
Ptrs->Next=FirstCell->Next;
free(FirstCell);
}
/* 计算堆栈的长度 */
int Length(Stack *Ptrs)
{
Stack *p;
p=Ptrs->Next;
int cnt =;
while(p)
{
cnt++;
p=p->Next;
}
return cnt;
}
/* 检验每一line是否符合要求 */
int check_stack(int *v,int N,int M)
{
int i=;
int num=;
Stack*ps;
ps=CreateStack();
Push(ps,); //先给栈中压入一个元素0
while(i<N) //审核每个已给元素
{ /* 核心 */ //入栈条件;当前栈顶元素小于已给数字,并且(前提条件)栈内元素的总容量小于栈的容量
while(ps->Next->Data<v[i] && Length(ps)<M+)
Push(ps,num++); //压入数num后,由于"the order is 1,2,……N。故下一个压入的数必为num+1;
if (ps->Next->Data==v[i])
{
Pop(ps); //栈顶元素出栈
i++; //之后的栈顶元素和下一个已给元素继续比较
}
else
{
free(ps);
return ;} //False!
}
free(ps);
return ; }
int main()
{
int M,N,K;
scanf("%d %d %d",&M,&N,&K); int *v=(int *)malloc(sizeof(int)*N);
int i,j;
for(i=;i<K;i++)
{
for (j=;j<N;j++)
{
scanf("%d",v+j);
}
if(check_stack(v,N,M))printf("YES\n");
else printf("NO\n");
}
free(v);
return ; }
1051. Pop Sequence的更多相关文章
- PAT 解题报告 1051. Pop Sequence (25)
1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- PAT 1051 Pop Sequence[栈][难]
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- 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 ...
- 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 ...
- PAT 甲级 1051 Pop Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...
- 【PAT】1051 Pop Sequence (25)(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 Advanced 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 ...
- 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 ...
随机推荐
- urlencode遇到中文编码问题
urlencode并不会改变输入的编码格式, 默认会将中文输出为 gbk 编码, 类似的, quote 会对中文进行 gbk 编码 不过, 当遇到嵌套多层的字典时, 问题就来了, 中文会被 utf8 ...
- tp5 model 中的查询范围(scope)
查询范围scope在model中定义,在controller中使用 namespace app\index\model; use think\Model; class User extends Mod ...
- webuploader在IE8/9下上传遇到的两个问题
最近在做图片上传功能. 点击一个按钮,弹出一个iframe,它是百度的webuploader插件 在点击关闭按钮时,IE9下总是会报错: __flash__removeCallback未定义错误 解决 ...
- Golang 语法学习笔记
Golang 语法学习笔记 包.变量和函数. 包 每个 Go 程序都是由包组成的. 程序运行的入口是包 main. 包名与导入路径的最后一个目录一致."math/rand" 包由 ...
- 【Java EE 学习 74 下】【数据采集系统第六天】【使用Jfreechart的统计图实现】【将JFreechart整合到项目中】
之前说了JFreechart的基本使用方法,包括生成饼图.柱状统计图和折线统计图的方法.现在需要将其整合到数据采集系统中根据调查结果生成三种不同的统计图. 一.统计模型的分析和设计 实现统计图显示的流 ...
- Redis到底该如何利用(三)?
上两篇受益匪浅,秉着趁热打铁,不挖到最深不罢休的精神,我决定追加这篇.上一篇里最后我有提到实现分级缓存管理应该是个可行的方案,因此今天特别实践了一下.不过缓存分级之后也发现了一些问题,例如下图: 当a ...
- 在WebStorm环境中给nodejs项目中添加packages
照前文 http://www.cnblogs.com/wtang/articles/4133820.html 给电脑设置了WebStorm的IDE的nodejs开发环境.新建了个express的网站 ...
- 【原创】内核ShellCode注入的一种方法
标 题: [原创]内核ShellCode注入的一种方法 作 者: organic 时 间: 2013-05-04,04:34:08 链 接: http://bbs.pediy.com/showthre ...
- OpenGL帧缓存对象(FBO:Frame Buffer Object)(转载)
原文地址http://www.songho.ca/opengl/gl_fbo.html 但有改动. OpenGL Frame BufferObject(FBO) Overview: 在OpenGL渲染 ...
- Nginx %00空字节执行php漏洞
Nginx如下版本:0.5.*, 0.6.*, 0.7 <= 0.7.65, 0.8 <= 0.8.37在使用PHP-FastCGI执行php的时候,URL里面在遇到%00空字节时与Fas ...