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> ...
随机推荐
- C#路径2
String apppath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; //获取整个文件路径名ap ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 引用
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字.一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量 C++ 引用 vs 指针 引用很容易与指针混淆,它们之间有三个主要的 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-map-marker
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- C# Process类详解
C# Process类详解 Process[] processes = Process.GetProcessesByName(current.ProcessName); 根据进程名字找到所有进程,返回 ...
- UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)
题意:问k小时后,第A~B行一共有多少个红气球. 分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式: (1)当cur <= POW[k - 1]时, dfs(k, cur) ...
- POJ1338 & POJ2545 & POJ2591 & POJ2247
POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结 POJ1338:Ugly Numbers Time Limit: 1000MS Memory Limit: 10 ...
- 当chart图遇上bootstrap的TAB切换 无宽高问题?
.tab-content > .tab-pane, .pill-content > .pill-pane { display: block; /* undo display:none ...
- java web实现在线编辑word,并将word导出(二)
前一篇文章介绍了后台将前台html转为word文档的一种方式,但却有一个问题是没法讲图片放置在生成的word报告中.我在网上找了很多方法,甚至将图片转换成base64编码的方式也不成功.效果如下: 由 ...
- 题解 P1220 【关路灯】
区间DP, 考虑设\(dp[i][j][t]\)为已经关掉了\([i,j]\)的电灯, 人在t端点处时的最小代价 可以推出方程: \[ dp[i+1][j][0]+(p[n]-p[j]+p[i])*( ...
- 小白学习之pytorch框架(7)之实战Kaggle比赛:房价预测(K折交叉验证、*args、**kwargs)
本篇博客代码来自于<动手学深度学习>pytorch版,也是代码较多,解释较少的一篇.不过好多方法在我以前的博客都有提,所以这次没提.还有一个原因是,这篇博客的代码,只要好好看看肯定能看懂( ...