PAT 1051 Pop Sequence[栈][难]
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
题目大意:给出一个栈最大容量,并且给出最大的N,要求是1....N是这样顺序入栈,输入K个检查的序列,需要判读是不是可能的弹出序列。
//我看见其实以为自己会,其实是不会的,以前见过这种题的,但是应该当时也没理解吧。
代码转自:https://www.liuchuo.net/archives/2232
#include <iostream>
#include <stack>
#include <vector>
#include<cstdio>
using namespace std;
int main() {
int m, n, k;
scanf("%d %d %d", &m, &n, &k);
for(int i = ; i < k; i++) {
bool flag = false;
stack<int> s;
vector<int> v(n + );
for(int j = ; j <= n; j++)
scanf("%d", &v[j]);//读入要检验的序列。
int current = ;//指向输入的序列。
for(int j = ; j <= n; j++) {
s.push(j);
if(s.size() > m) break;
while(!s.empty() && s.top() == v[current]) {
s.pop();
current++;
}
}
if(current == n + ) flag = true;
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
//真的很厉害了,学习了。
1.使用一个current来指向当前的检验序列。
2.如果栈的大小已经大于了容量,那么就退出。
3.还有这个while循环是最关键的,只要top值等于当前current指向的,那么就弹出,并且指向下一个元素,非常厉害了。
//学习了,另一位大佬的代码思路也是相同的。
PAT 1051 Pop Sequence[栈][难]的更多相关文章
- PAT 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- PAT 1051 Pop Sequence
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; bool 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 ord ...
- 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 ...
- Pop Sequence (栈)
Pop Sequence (栈) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...
- 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 ...
- PAT 甲级 1051 Pop Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...
- 【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 ...
- 1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...
随机推荐
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- CSS3 实现的一个简单的"动态主菜单" 示例
其实这个示例蛮无聊的 很简单 也没什么实际的用处. 主要是展示了 CSS3 如何实现动画效果. 写这个主要是想看一看 完成这样的效果 我到底要写多少代码. 同时和我熟悉的java做个比较. 比较结果不 ...
- [浪风分享] PHP开发必看 我现在是这样编程的
我在做什么 曾经,我试过接到一些需求.一眼带过后,脑袋马上随着高昂的斗志沉溺在代码的世界中 ,马不停蹄地敲着键盘直到最后测试的完成.我从思绪中恢复过来,乍一看自己写的功能,和需求差了十万八千里,我TM ...
- VS2008设置快捷键Ctrl+W关闭当前打开的文本编辑器窗口
好多友好的软件关闭多标签页的当前页时都有Ctrl+W的快捷键,如Chrome浏览器,使用起来还是很方便的. 但是作为程序员,使用VS2008时有时会打开好多C++或C#源文件,需要关闭某个源文件时你需 ...
- ConfigParser 读写配置文件
一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...
- C static 关键字理解
今天来看一下这么一个程序. #include<stdio.h> int count =1; int fun(void) { static int count =10; return cou ...
- Fel初认识
Fel在源自于企业项目,设计目标是为了满足不断变化的功能需求和性能需求. Fel是开放的,引擎执行中的多个模块都可以扩展或替换.Fel的执行主要是通过函数实现,运算符(+.-等都是Fel函数),所有这 ...
- ios开发-获取手机相关信息
今天在做客户端的时候,里面有个意见反馈功能. 调用系统带的邮件功能,发送邮件到指定邮箱. 然后我就想,应该在邮件正文部分添加手机相关内容,比如型号,版本,应用程序的版本等等,这样不仅使用者方便,开发者 ...
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
Java代码 <!-- 指明spring配置文件在何处 --> <context-param> <param-name>contextConfigLocation& ...
- Hadoop 启动脚本分析与实战经验
start-all.sh脚本现在已经废弃,推荐使用start-dfs.sh和start-yarn.sh分别启动HDFS和YARN. 在新一代的Hadoop里面HDFS称为了统一存储的平台,而YARN成 ...