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 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~N依次入栈,在入栈过程中,由p指针指向序列中当前等待出栈的元素
- 如果栈顶元素恰好等于序列当前等待出栈的元素,让栈顶元素出栈,p指针后移
- 继续循环2的操作,知道栈顶元素不等于序列中当前等待出栈的元素
- 继续1~N的压入
- 判断是否为出栈顺序
- 如果压栈过程中栈大小>=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) [栈模拟]的更多相关文章
- 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)(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 解题报告 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 (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 ...
- 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 ...
- PAT 甲级 1051 Pop Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...
- 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)
题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...
- PAT (Advanced Level) 1051. Pop Sequence (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
随机推荐
- json字符串格式化显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- inkscape批量将svg转为pdf
for i in *.svg;do inkscape --export-pdf=${i%.*}.pdf $i;done
- 【剑指Offer】面试题52. 两个链表的第一个公共节点
题目 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- mybaits的注解方式与配置文件方式比较
注解方式比配置文件方式简单了更多. 俩种配置 一个是制定配置文件路径,一个直接class指向dao层接口 只需加一个注解@**** 就能够实现 比写一个xml的配置文件简单更多.
- stl 题目总结
stl 题目总结 一.圆桌问题 1 .问题: 圆桌上围坐着2n个人.其中n个人是好人,另外n个人是坏人.如果从第一个人开始数数,数到第m个人,则立即处死该人:然后从被处死的人之后开始数数,再将数到的第 ...
- PHP - 接收检验验证码 , 以JSON的形式将验证结果返回给前端
<?php session_start(); $codeT = strtoupper(trim($_POST['codeT']));//接收前端传来的数据,转换成大写 $raw ...
- BGP(IBGP“内部路由器”和EBGP“外部路由器”)命令解析
BGP:基于策略的路径向量路由协议. ①:(attribute)属性描述路径. ②:使用TCP(端口179)作为传输协议——(IBGP多使用loopback端口建立update-source) IBG ...
- 大二暑假第五周总结--开始学习Hadoop基础(四)
简单学习MapReduce并进行WordCount实践 分布式并行编程: MapReduce设计的一个理念就是“计算向数据靠拢”,将复杂的,运行于大规模集群上的并行计算过程高度地抽象到两个函数:Map ...
- 快速幂(51Nod1046 A^B Mod C)
快速幂也是比较常用的,原理在下面用代码解释,我们先看题. 51Nod1046 A^B Mod C 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. In ...
- 初识MyBatis-Generator
详细请见: http://www.mybatis.org/generator/quickstart.html 使用mybatis-generator-core-x.x.x.jar加上配置文件来生成 1 ...