PAT 甲级 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 分)(模拟栈,较简单)的更多相关文章
- 1051 Pop Sequence (25分)栈
刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...
- 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)
题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...
- 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 (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 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 ...
- 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 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- 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 ...
随机推荐
- flask中使用ajax 处理前端请求 弹框展示
菜小鱼初次使用 ajax,想前端提交数据,后端处理后,将结果以弹框的形式展示,在网上查看了好多,不停的调试,终于调通了 html: <html> <head></head ...
- Linux下干净卸载mysql
1.首先查看mysql的安装情况 rpm -qa|grep -i mysql 显示之前安装了: MySQL-client-5.5.25a-1.rhel5 MySQL-server-5.5.25a-1. ...
- hive 常用操作
参考:https://www.cnblogs.com/jonban/p/10779938.html Hive 启动:hive 退出:hive>quit; show databases; use ...
- 远程jupyter+pycharm配置
前言 作为一个数据工程师,最喜欢的事情就是不停的在现实的服务端环境调试代码的参数.我们在本地的环境与生产的状况决然不同,我们一开始在本地写代码测试再部署到服务端的三板斧就不适用了. 最好的方式可以直接 ...
- Spring第二次案例和AOP
Spring加上AOP com.mapper.entity.UserInfo package com.Spring.entity; public class UserInfo { private In ...
- IDEA控制台中文乱码解决
关于IDEA中文乱码的解决方法,如下. 1.打开idea安装目录,选择 打开文件,末尾添加-Dfile.encoding=UTF-8 2.打开IntelliJ IDEA>File>Sett ...
- 三十二. 多表查询 MySQL管理工具 、 用户授权及撤销
1.MySQL管理工具 部署LAMP+phpMyAdmin平台 安装httpd.mysql.php-mysql及相关包 启动httpd服务程序 解压phpMyAdmin包,部署到网站目录 配置conf ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- wepy快速入门
Github地址 wepy官网:https://tencent.github.io/wepy/index.html 安装 wepy 命令行工具 npm install wepy-cli -g 在开发目 ...
- ZR#988
ZR#988 解法: 先算出横着能排多少座位, 以及需要排几列, 才能把 n 个座位全部排下来.要使得尽量多的位置在走廊边上, 于是在 n 列中插入走廊的策略是显然的, 我们只要以两列为单位, 在其中 ...