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 ...
随机推荐
- 抹掉Scala的糖衣(14) -- Update Method
欢迎关注我的新博客地址:http://cuipengfei.me/ 在Scala中,名字叫做update的方法是有特殊作用的. 比如: 1 2 3 val scores = new scala.col ...
- [转载]Jenkins持续集成项目搭建与实践——基于Python Selenium自动化测试 -2
自己的代码 import unittest # import HTMLTestRunner_cn as HTMLTestRunner import xmlrunner import sys sys.p ...
- 【BZOJ】1691: [Usaco2007 Dec]挑剔的美食家(set+贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1691 懒得打平衡树了.... 而且multiset是很快的... 排到了rank1 T_T 贪心就是 ...
- 【BZOJ】1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏(floyd)
http://www.lydsy.com/JudgeOnline/problem.php?id=1641 这种水题无意义... #include <cstdio> #include < ...
- 关于Java中的HashMap的深浅拷贝的测试与几点思考
0.前言 工作忙起来后,许久不看算法,竟然DFA敏感词算法都要看好一阵才能理解...真是和三阶魔方还原手法一样,田园将芜,非常可惜啊. 在DFA算法中,第一步是需要理解它的数据结构,在此基础上,涉及到 ...
- sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)
Ivan comes again! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...
- 一稿设计多端适配优雅的解决方案 - rem
规范目的 为提高前端团队开发效率,输出高质量的前端页面代码,提高UI设计还原度,特编写该规范文档.本文档如有不对或者不合适的地方请及时提出. JS代码块 (function (doc, win) { ...
- Linux命令之乐--rename
用来修改文件名.重命名文件,批量重命名文件rename是最好的选择. 用法:rename from to files... [root@Director test]# ls a_01 a_02 [ro ...
- Java Web项目BlogAutoGenerator编写日志1
使用的数据库是MySQL,所以首先要在MySQL中创建database和表.建立数据库article_db,建立表blog,如下: create database article_db DEFAULT ...
- 如何使用 awk 复合表达式
导读 一直以来在查对条件是否匹配时,我们使用的都是简单的表达式.那如果你想用超过一个表达式来查对特定的条件呢?本文中,我们将看看如何在过滤文本和字符串时,结合多个表达式,即复合表达式,用以查对条件. ...