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

题意:

给定一个有固定容量的栈,1,2,...,n是入栈序列,元素出栈顺序随意,现给定出栈顺序(e.g.1~n的一个排列),问这个出栈顺序是否合理,合理输出"YES",否则输出"NO"。

题解:

开一个队列,开一个栈,输入一个数,就队列中这个数及之前的数放入栈中,放入不能超过容量。然后看栈顶元素是不是这个数,是就下一个,不是就标记NO。

AC代码:

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int n,m,k;
stack<int>s;
queue<int>q;
int main(){
cin>>n>>m>>k;
while(k--){
while(!s.empty()) s.pop();
while(!q.empty()) q.pop();
int f=;
for(int i=;i<=m;i++) q.push(i);
for(int i=;i<=m;i++){
int x;
cin>>x;
if(s.empty()||s.top()!=x){
while(!q.empty()){
if(s.size()<n) {
//cout<<"把"<<q.front()<<"放栈"<<endl;
s.push(q.front());
q.pop();
}
else break;
if(s.top()==x) break;
}
}
if(s.top()==x){
s.pop();
//cout<<x<<"踢出栈"<<endl;
continue;
}
f=;
}
if(f) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)的更多相关文章

  1. 1051 Pop Sequence (25分)栈

    刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...

  2. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  3. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  4. 【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 ...

  5. 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 ...

  6. 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 ...

  7. PAT 甲级 1051 Pop Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...

  8. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  9. 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 ...

随机推荐

  1. 【loj-1055-Going Together-三个棋子推箱子走到目的地--讲预判的bfs】

    light oj 1055-Going Together 题目大致意思: 简单的三个棋子,每次可以下达一个命令,robots全部按照指令进行前进:若下一步不为空地则停留在原地. 特殊考虑: 1.例如A ...

  2. 模拟webpack 实现自己的打包工具

    本框架模拟webpack打包工具 详细代码个步骤请看git地址:https://github.com/jiangzhenfei/easy-webpack 创建package.json { " ...

  3. linux 防火墙 firewall 设置

    背景: 机房断电后导致机器关机重启,原先访问的地址访问不了,使用终端能访问到该服务器,服务启起来后,用curl + 地址能访问,但在外部浏览器访问不了该地址: 首先想到了端口限制----防火墙 参考博 ...

  4. Selenium常用API的使用java语言之2-环境安装之IntelliJ IDEA

    1.安装IntelliJ IDEA 你可能会问,为什么不用Eclipse呢?随着发展IntelliJ IDEA有超越Eclipse的势头,JetBrains公司的IDE基本上已经一统了各家主流编程语言 ...

  5. GeoJSON与GeoBuf互相转换

    GeoJSON格式通常比较大,网页需要较长时间加载,可以使用GeoBuf进行压缩. 使用GeoBuf有很多好处:结构紧凑.文件小.方便编码和解码.能适用各种GeoJSON等等. 使用: 1.安装 ge ...

  6. 使用Newtonsoft.Json将数据导出至Json文件

    导出方法: /// <summary> /// 导出文件 /// </summary> public void Export(List<Dict> dicts, s ...

  7. jQuery相关方法6----三大系列属性

    一.获取和设置元素的宽和高------width( )方法和height()方法 <!-- 点击按钮,设置div的宽和高为原来的两倍 --> <script src="ht ...

  8. 扩展kmp学习笔记

    kmp没写过,扩展kmp没学过可还行. 两个愿望,一次满足 (该博客仅用于防止自己忘记,不保证初学者能看懂我在瞎bb什么qwq) 用途 对于串\(s1,s2\),可以求出\(s2\)与\(s1\)的每 ...

  9. 数据结构实验之图论六:村村通公路【Prim算法】(SDUT 3362)

    题解:选点,选最小权的边,更新点权.可以手动自行找一遍怎么找到这个最小的生成树,随便选一个点放入我们选的集合中,然后看和这个点相连的点中,与那个点相连的那条边权值是最小的,选择之后,把相连的这个点一起 ...

  10. 2019 ICPC 沈阳网络赛 J. Ghh Matin

    Problem Similar to the strange ability of Martin (the hero of Martin Martin), Ghh will random occurr ...