原题连接: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的更多相关文章

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

  2. PAT 1051 Pop Sequence[栈][难]

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

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

  6. PAT 甲级 1051 Pop Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...

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

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

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

随机推荐

  1. Reverse Core 第一部分 代码逆向技术基础

    @date: 2016/10/14 <逆向工程核心原理>笔记 记录书中较重要的知识,方便回顾 ps. 因有一些逆向基础,所以我本来就比较熟悉的知识并未详细记录 第一章 关于逆向工程 目标, ...

  2. BigZhuGod的粉丝

    BigZhuGod的粉丝 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  3. CPrimerPlus第11章第10题

    题目: 编写一个程序,读取输入,直到读入了10个字符串或遇到EOF,由二者中最先被满足的那个终止读取过程.这个程序可以为用户提供一个有5个选项的菜单:输出初始字符串列表.按ASCII顺序输出字符串.按 ...

  4. Redis安装及实现session共享

    一.Redis介绍 1.redis是key-value的存储系统,属于非关系型数据库 2.特点:支持数据持久化,可以让数据在内存中保存到磁盘里(memcached:数据存在内存里,如果服务重启,数据会 ...

  5. 阿里云windows 2012服务器部署java web程序教程

    一:环境搭建 1.首先需要购买一个阿里云ECS服务器,购买时可以选择处理器核数以及内存大小(可以购买偏低配置,因为升级      方便) 2.购买后会自动创建一个实例,可以使用该实例中显示的公网地址在 ...

  6. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  7. php性能剖析的几款软件

    1. xhprof (http://pecl.php.net/package/xhprof)   facebook  2009年开源 2. xdebug 3. valgrind 4. cachegri ...

  8. C#并行

         /// <summary>         ///该实现方式并不是最高效的         ///只是举个例子,说明用锁来保护共享状态         /// </summ ...

  9. Java Graphics2D 画出文字描边效果

    在CSDN看到的,在此记下. (http://bbs.csdn.net/topics/390703095) import javax.swing.*; import java.awt.*; impor ...

  10. 本人为巨杉数据库(开源NoSQL)写的C#驱动,支持Linq,全部开源,已提交github

    一.关于NoSQL的项目需求 这些年在做AgileEAS.NET SOA 中间件平台的推广.技术咨询服务过程之中,特别是针对我们最熟悉的医疗行业应用之中,针对大数据分析,大并发性能的需求,我们也在慢慢 ...