题目

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

题目分析

已知一个栈限制容量最大为M,push操作压入1,2,3...N,已知K个序列,判断序列是否可能是出栈顺序

解题思路

  1. 1~N依次入栈,在入栈过程中,由p指针指向序列中当前等待出栈的元素
  2. 如果栈顶元素恰好等于序列当前等待出栈的元素,让栈顶元素出栈,p指针后移
  3. 继续循环2的操作,知道栈顶元素不等于序列中当前等待出栈的元素
  4. 继续1~N的压入
  5. 判断是否为出栈顺序
    • 如果压栈过程中栈大小>=M,说明栈满,该序列不是出栈顺序,退出;
    • 如果压栈执行结束后,栈中有剩余元素或者p指针未指向序列末尾,证明该序列不是出栈顺序

Code

Code 01

#include <iostream>
#include <stack>
using namespace std;
int main(int argc,char * argv[]) {
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int seq[m]= {0};
for(int i=0; i<k; i++) {
for(int j=0; j<m; j++) {
scanf("%d",&seq[j]);
}
stack<int> sk;
int j,p = 0;
for(j=1; j<=m; j++) {
if(sk.size()>=n)break;
sk.push(j);
while(!sk.empty()&&sk.top()==seq[p]) {
sk.pop();
p++;
} }
if(j<=n||!sk.empty())printf("NO\n");
else printf("YES\n");
}
return 0;
}

PAT Advanced 1051 Pop Sequence (25) [栈模拟]的更多相关文章

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

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

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

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

  7. PAT 甲级 1051 Pop Sequence

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

  8. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  9. PAT (Advanced Level) 1051. Pop Sequence (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

随机推荐

  1. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

  2. JAVA中序列化和反序列化中的静态成员问题

    关于这个标题的内容是面试笔试中比较常见的考题,大家跟随我的博客一起来学习下这个过程. ? ? JAVA中的序列化和反序列化主要用于: (1)将对象或者异常等写入文件,通过文件交互传输信息: (2)将对 ...

  3. SOA--基于银行系统实例分析

    阅读以下关于 Web 系统设计的叙述 [说明] 某银行拟将以分行为主体的银行信息系统,全面整合为由总行统一管理维护的银行信息系统,实现统一的用户账户管理.转账汇款.自助缴费.理财投资.贷款管理.网上支 ...

  4. 每天一点点之 taro 框架开发 - 事件处理与样式表

    1.方法调用 state = { name:'张三' } test(){ this.state.name } <button onClick={ this.test.bind(this) } / ...

  5. C语言-逃逸字符、类型转换和布尔类型

    C语言-逃逸字符 逃逸字符是用来表达无法印出来的控制字符或者特殊字符,它由一个反斜杠""开头,后面跟上另一个字符,这两个字符合起来,组成一个字符. \b是backspace,在su ...

  6. [DDCTF 2019]homebrew event loop

    0x00 知识点 逻辑漏洞: 异步处理导致可以先调用增加钻石,再调用计算价钱的.也就是先货后款. eval函数存在注入,可以通过#注释,我们可以传入路由action:eval#;arg1#arg2#a ...

  7. 2020/1/30 PHP代码审计之文件上传漏洞

    0x00 漏洞简介 文件上传漏洞是指用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务器端命令的能力.这种攻击是最为直接和有效的,"文件上传"本身是没有问题,有问题的是 ...

  8. python查找数组中出现次数最多的元素

    方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...

  9. POJ 1125:Stockbroker Grapevine

    Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64 ...

  10. 进度3_家庭记账本App_Fragment使用SQLite实现简单存储及查询

    AddFragment.java: package com.example.familybooks; import android.content.ContentValues; import andr ...