博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
02-线性结构4 Pop Sequence(25 分)
阅读量:4658 次
发布时间:2019-06-09

本文共 2026 字,大约阅读时间需要 6 分钟。

02-线性结构4 Pop Sequence(25 分)

标签(空格分隔): 数据结构 C++


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 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 51 2 3 4 5 6 73 2 1 7 5 6 47 6 5 4 3 2 15 6 4 3 7 2 11 7 6 5 4 3 2Sample Output:YESNONOYESNO

看了半天才看明白,其实和当初做的那道经典的UVA514 Rail是一样的。不一样的地方就是现在的栈有最大容量。只需要写一个栈的模拟就行了。

此外,还需要注意的就是边界条件了。这里的边界条件,就是程序跳出循环的点。YES的条件很好找,只需要i遍历全部元素就行了。但是NO呢?这时考虑到,由于每次元素都需要进栈,之后再判断该元素是否等于目标元素,所以只需要判断栈空间满了的时候,栈顶元素是否等于目标序列对应元素就行了,如果不等,就输出NO。

#include 
#include
#include
#define N 1003using namespace std;int main() { int m,n,k; scanf("%d %d %d",&m,&n,&k); while (k--) { int aim[N],i=1,j=1; stack
st; for (int ii=1;ii<=n;ii++) scanf("%d",&aim[ii]); while (1) { if (st.empty()) {st.push(j);j++;} else if (!st.empty()&&st.top()==aim[i]) {st.pop();i++;} else {st.push(j);j++;} if (i>n) {
printf("YES\n");break;} else if (st.size()>=m) { if (st.top()!=aim[i]) { printf("NO\n"); break; } } } } return 0;}

转载于:https://www.cnblogs.com/yichuan-sun/p/9624177.html

你可能感兴趣的文章
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>
Django Rest Framework -解析器
查看>>
ExtJs 分组表格控件----监听
查看>>
Hibernate二级缓存配置
查看>>
LoadRunner常用术语
查看>>
关于jedis2.4以上版本的连接池配置,及工具类
查看>>
记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
查看>>
FactoryBean
查看>>