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> ...
随机推荐
- 060-PHP函数定义和调用
<?php function add($x,$y){ //定义函数add return $x+$y; } echo add(15,6); //调用函数并输出结果 ?>
- mysql union 和union all
http://www.w3school.com.cn/sql/sql_union.asp
- Java 统计整数二进制中1的个数
package cookie; public class CountBinary_1 { public static void main(String[] args) { System.out.pri ...
- SPOJ RENT 01背包的活用+二分
这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...
- maven详解之仓库
在Maven中,任何一个依赖.插件或者项目构建的输出,都可以称之为构件. Maven在某个统一的位置存储所有项目的共享的构件,这个统一的位置,我们就称之为仓库.(仓库就是存放依赖和插件的地方) 任何的 ...
- jsp页面使用<% 语句%> SQL Server数据库报空指针异常(在控制台可以正常执行)
一直反感用SQL Server数据库,很影响电脑性能!!数据库作业不得不用 前几天作业一直报空指针异常: 自己检查了所传参数,和数组不为空 数据库查询语句不为空 然后查看SQL服务是否启动 主要是S ...
- 哈希(hash)理解
转载自https://www.cnblogs.com/mingaixin/p/4318837.html 一.什么是哈希?(一种更复杂的映射) Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就 ...
- 代理模式(Proxy Pattern)C#版本的
引用地址 https://www.cnblogs.com/zhili/p/ProxyPattern.html --------------------------------------------- ...
- Beyond Compare 文件对比工具的使用
Beyond Compare 文件对比工具的使用 Beyond Compare 工具下载地址: http://www.onlinedown.net/soft/633850.htm 本文下载地址:E:\ ...
- cf1228 D Complete Tripartite(哈希)
题意: 无向简单图,无自环,无重边,n个点,m条边,请你将这n个点分为3个互相没有交集的集合.并且满足以下条件: 1.同一个集合中的任意两点之间没有边. 2.每个点都要与除了它这个集合以外的所有点相连 ...