Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分)
Question: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:
Sample Output:
YES
NO
NO
YES
NO
分析:此题考察栈的操作,入栈的顺序是1,2,3......,N。出栈序列以5 6 4 3 7 2 1为例,要pop 5,就必须先push 1, push 2, push 3, push 4, push5, 此时栈顶元素为5,刚好匹配,才能进行pop操作。这里首先清空栈,设置一个将要入栈的顺序值temp,由1开始自增。当栈顶元素与读取的出栈序列值不匹配(还要考虑栈空的情况)时就进行入栈操作: Sta.push(temp++); ;当栈顶元素与读取的出栈序列值匹配,要进行出栈操作 Sta.pop(); 弹出栈顶元素,再读取下一个出栈序列值。如果栈中的元素个数超过了M,则说明出现了错误,这种出栈序列是不成立的。
源码:
#include<iostream>
#include<stack> //调用C++ STL中的堆栈容器
using namespace std; int main()
{
int M, N, K;
int obtain, pop; // obtain为将要入栈的顺序值(1,2,..,N),pop为当前读取的出栈序列值
bool is_failed; // 出栈序列成立与否的标志
stack<int> sta;
cin >> M >> N >> K;
for (int i = ; i < K; i++) // 循环输入K组待判定的出栈序列
{
is_failed = false;
obtain = ;
for (int j = ; j < N; j++) // 循环读取每个出栈序列值
{
cin >> pop;
while (sta.size() <= M && !is_failed) // 栈未满且未确认出栈序列不成立
{
if (sta.empty() || pop != sta.top()) // 栈为空或当读取的出栈序列值与栈顶元素不相等时,把顺序值temp压栈并递增
{
sta.push(obtain++);
}
else // 当前读取的出栈序列值与栈顶元素相等时出栈,跳出循环继续读取下一个出栈序列值
{
sta.pop();
break;
}
}
if (sta.size() > M)
{
is_failed = true; // 确认出栈序列不成立
}
}
if (is_failed) cout << "NO" << endl;
else cout << "YES" << endl;
while (!sta.empty()) sta.pop(); // 清空栈,因为下一次匹配还要用
}
return ;
}
Pop Sequence的更多相关文章
- 1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...
- 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 ...
- 02-线性结构3 Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- Pop Sequence (栈)
Pop Sequence (栈) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...
- 数据结构练习 02-线性结构3. 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 ...
- 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 ...
- PAT1051:Pop Sequence
1051. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
- A1051. Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- 数据结构习题Pop Sequence的理解----小白笔记^_^
Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...
随机推荐
- Js验证userAgent是否来自手机端
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- druid 源码分析与学习(含详细监控设计思路的彩蛋)(转)
原文路径:http://herman-liu76.iteye.com/blog/2308563 Druid是阿里巴巴公司的数据库连接池工具,昨天突然想学习一下阿里的druid源码,于是下载下来分析了 ...
- cocosbuilder3.0使用小记
新项目用到了堪称完美的cocos2d-x2.1.5版本,用cocsbuilder2.1版本出现了返回的最终node为null的问题,看xcode的提示说: cocos2d: WARNING! Inco ...
- Debian下安装Firefox与flash简介
Debian下安装Firefox与flash简介 由于Debian在Firefox的版权上出现了问题,导致官方发布的Debian系统不能使用默认的Firefox浏览器,最后官方重编的Firefox改名 ...
- Core Java Volume I — 3.4. Variables
3.4. VariablesIn Java, every variable has a type. You declare a variable by placing the type first, ...
- 第一部分 python基础
1.1,参数传递:*代表元组,**代表字典 1.2 ,常用数据类型 列表 [1,2,3] 元组 (1,2,3) 不可变的list 集合 {1,2,3} 字典 {1:a, 2:c} (4)字典以关键字为 ...
- CSS+DIV常用命名
常用的符合CSS+DIV规则的命名 页头:header 登录条:loginBar 标志:logo 侧栏:sideBar 广告:banner 导航:nav 子导航:subNav 菜单:menu 子菜单: ...
- Linux软件安装管理概述
介绍如何在Linux字符界面下安装软件 课程大纲: 一.软件包管理简介 二.rpm命令管理 三.yum在线管理 四.源码包管理 五.脚本安装包
- Java基础了解
今天刚开始学习Java,除了老师讲的之外,又进一步上网去了解了下Java的相关知识: Java语言的主要特点: 1. 跨平台性 所谓的跨平台性,是指软件可以不受计算机硬件和操作系统的约束而在任意计算机 ...
- 为什么说Parcelable 比Serializable更高效
本文转载自:http://blog.csdn.net/androidzhaoxiaogang/article/details/8172539 什么是序列化,实现序列化的目的是什么? 讨论这个问题之前, ...