题目

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. 一百零七、SAP的OO-ALV之一,新建程序

    一.来带SE38模块,新建一个Z_TIANPAN_20190807_OOALV的本地程序 二.设置一个标题,点击对勾 三.选择保存为本地对象 我们下一篇来写创建屏幕

  2. 159-PHP strstr函数,取最后几个字符和除去最后几个字符

    <?php $str='programming'; //定义一个字符串 $res=substr($str,-4); //处理字符串 echo "返回{$str}字符串的最后4个字符{$ ...

  3. React全局浮窗、对话框

    下面代码是组件源码: import React, {Component} from 'react' import {createPortal} from 'react-dom' import styl ...

  4. JS最新最细面试题

    转之:https://www.jianshu.com/p/f1f39d5b2a2e 1. javascript的typeof返回哪些数据类型. 答案:string,boolean,number,und ...

  5. 10 ~ express ~ 使用 cookie 保存用户 信息

    思维导图: (1) 保存 cookie (2)销毁 cookie 一,保存 cookie 1,app.js  . 新增代码 var Cookies = require('cookies') /** * ...

  6. 19 01 16 jquery 的 属性操作 循环 jquery 事件 和事件的绑定 解绑

    jquery属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').htm ...

  7. 【LeetCode】解数独

    做题之前先复习下[STL中的Tuple容器] 我们知道,在Python中,大家都知道tuple这个概念,是一个只读的元素容器,容器内的元素数据类型可以不同,而在CPP中大部分的容器只能储存相同数据类型 ...

  8. hibernate.hbm.xml必须必须配置主键

    hibernate.hbm.xml必须必须配置主键 <id name="XXid" type="java.lang.long" column=" ...

  9. POJ-3262 贪心的一个小技巧

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3204   Accepted: ...

  10. ACM&OI 基础数学算法专题

    [前言] 本人学习了一定时间的算法,主要精力都花在数学类的算法上面 而数学类的算法中,本人的大部分精力也花费在了数论算法上 此类算法相对抽象,证明过程比较复杂 网络上的博客有写得非常好的,但也有写得不 ...