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, ...
随机推荐
- 转:Repeater嵌套绑定Repeater以及内层调用外层数据
<table border=" style="margin-bottom: 5px" width="100%"> <asp:Repe ...
- 在KCloud上轻松“玩转”Docker
继CoreOS和Atomic镜像上线之后,刻通云紧跟Docker技术发展脚步,近期又推出了Ubuntu Core镜像,成为国内首家支持Ubuntu Core镜像的基础云服务商,同时也是国内唯一一家同时 ...
- 双系统安装要点 - imsoft.cnblogs
1.用磁盘工具 取消当前激活分区,并隐藏当前激活分区2.按照普通的形式安装系统 Ghost安装和简单安装都可以3用修复启动项工具 修复之前处隐藏的系统启动项 OK,再就不会看到烦人的蓝屏了!
- UVa 1388 - Graveyard
题意:有一个周长为10000的圆上等距分布着n个雕塑,现在又加入m个雕塑,位置随意,希望n+m个雕塑仍然均匀分布.这就要移动其中一些雕像,求移动的最小距离. 这个题的方法很巧妙,首先将整个圆分成(m+ ...
- iOS学习笔记---oc语言第一天
第一讲 初始类和对象 c语言的超集,允许在oc中使用c语言源代码.编译器兼容c语言程序 具备完善的面向对象特性 包含一个运行时系统 类库丰富 面向对象编程 oop 面向对象语言:c++ java ...
- 一个textview实现文字在图片上面的效果
类似于这样的,其实很简单,可是以前用的是imageview+textview.布局实现多写了好多代码.又不能在图片加文字,显得没技术含量. xml代码如下: <TextView android: ...
- (单选后,显示相对应的div)点击免费没有变化,点击收费出现输入框
<li> <label class="feiyonglabel">活动费用</label> <div class="textbo ...
- Fragment中调用Activity的UI
1:Fragment内定义一个接口,或外部定义一个接口, 2:Fragment定义一个接口对象的属性 3:Activity实现这个接口 4:Fragment的onAttach方法中强转Activity ...
- Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- my Style
1. box-sizing语法: box-sizing : content-box || border-box || inherit 参数取值: content-box:此值为其默认值,其让元素维持W ...