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 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.要pop n,前提是要push 1 2 3 … n-1。 num为既定序列1 2 3 … 例如,input为 4 3 2 1的4时,首先要push 1 2 3 才能push 4 pop 4 出栈。
2.stack.size() > M(the maximum capacity of the stack) 超出栈容量不可能。
思路:当栈顶元素不是input 则push(num++)直到input,随后pop input;或者stack.size() > M(the maximum capacity of the stack) 检测出不可能。
#include<iostream>
#include<stack>
using namespace std; int main()
{
int M; //maximum capacity of the stack
int N; //the length of push sequence
int K; //the number of pop sequence to be checked
cin >> M >> N >> K;
bool flag = true;
int input, num; //num= 1.2.3.4.5... input为依次输入的检测序列值
stack<int> sta; for(int i = ; i < K; i++) {
num = ;
flag = true;
for(int j = ; j < N; j++) {
cin >> input;
while( sta.size() <= M && num ) {
if(sta.empty() || sta.top() != input) {
sta.push(num ++);
}else if(sta.top() == input) {
sta.pop();
break;
}
}
if(sta.size() > M ) //超出栈容量
flag = false;
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl; while(!sta.empty()) //清空栈
sta.pop();
}
return ;
}
参考:http://www.2cto.com/kf/201311/254409.html 开始误入了比大小的误区,这个比较细致易理解。记一笔。
02-线性结构3 Pop Sequence的更多相关文章
- 线性结构4 Pop Sequence
02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...
- 数据结构练习 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 ...
- 02-线性结构4 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 ...
- PTA 02-线性结构4 Pop Sequence (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence (25分) Given a stack wh ...
- 02-线性结构4 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 ...
- 02-线性结构4 Pop Sequence
题目 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 ...
- [刷题] PTA 02-线性结构4 Pop Sequence
模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...
随机推荐
- html+css源码之实现登录弹出框遮罩层效果
在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...
- php 判断数组是否为空的几种方法
php数组为空我们可以通过很多php自带函数来实现了,比如count函数,empty函数等都可以判断数组是否为空,下面小编就为各位整理了不少的判断数组是否为空的函数,希望对各位会带来帮助. PHP判断 ...
- J1签证办理全过程
从3月底开始申请,整理J1的材料,一直到现在VISA的status变成了issued,中间被check了20多天,终于快要可以去silicon valley了. 废话不多说,J1签证很easy,不要有 ...
- Python计算程序运行时间
方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.no ...
- Java Web架构知识整理——记一次阿里面试经历
惭愧,从一次电面说起.我个人在某国企做一名软件设计师,国企大家都懂的,待遇一般而且没啥意思,做的方向基本都是操作系统.驱动和工具软件的开发,语言基本都是C/C++.最近也想跳槽,刚好有幸得到了一次阿里 ...
- 解决setInterval计时器不准的问题
在js中如果打算使用setInterval进行倒数,计时等功能,往往是不准确的,因为setInterval的回调函数并不是到时后立即执行,而是等系统计算资源空闲下来后才会执行.而下一次触发时间则是在s ...
- 慕课网-安卓工程师初养成-2-5 如何命名Java变量
来源:http://www.imooc.com/code/1221 如同酒店会给每个房间起个性化的名字一样,程序中的变量也需要用合理的名字进行管理---变量名! 需要注意,给酒店房间起名字时可以是数字 ...
- Andriod项目开发实战(1)——如何在Eclipse中的一个包下建新包
最开始是想将各个类分门别类地存放在不同的包中,所以想在项目源码包中新建几个不同功能的包eg:utils.model.receiver等,最后的结果应该是下图左边这样的: 很明显建立项目后的架构是上 ...
- CLRS:median and order statistics
//maximum and minimum 暴力遍历 O(n) //i-th element dicide and conquer random_selected_partition ...
- adb调试命令详解-2016.02.01
adb(Android Debug Bridge),调试桥可以让设备的调试监测过程在远端进行,而不必在运行实际运行应用的设备上,方便调试的输出. 1 命令详解 a 查看帮助信息 adb ...