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]; ...
随机推荐
- spark1.2.0版本SparkSQL使用parquet类型注意事项
在Spark1.2.0版本中是用parquet存储类型时注意事项: sql语句: select * from order_created_dynamic_partition_parquet; 在spa ...
- VC 三点 划 曲线
y = ax2+bx+c 条件,三点成一曲线 pointone(x1,y1)//(y1在X柱上,必须为零,如果不为零这个公式要重新求值) pointtwo(x2,y2)// 可以为任意值 pointt ...
- Iptables 防火墙开放常见的22,53,80端口
用iptables防火墙 iptables -F # 允许包从22端口进入 iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许从22端口进入的包返回 ...
- mysql创建远程用户
grant all privileges on *.* to myuser@"%" identified by 'password'; 用root用户登陆,然后: grant al ...
- 翻译的技巧(The Technique of Translation)——前言
曰:你要发文章才能毕业! 答:谨遵圣命-- 缘起 研三了,要找工作了,要离开学校了,又要像浮萍般飘荡在世上了,还是学校好呀!本以为写个毕业论文就可以了,谁知老板幽幽地说写篇文章发了吧.我竟无言以对.不 ...
- sqlserver 常用sql语句
SELECT COUNT(*) FROM WeixinUser SELECT COUNT(*) FROM WeixinUser WHERE datediff(day, CreateTime,getda ...
- HTTP 错误 500(Internal Server Error)
今天在用ajax请求页面的时候出现了这么一个错误:HTTP 错误 500(Internal Server Error) 由于提示较少,过了好一阵子才找到答案:ajax请求中调用了一个不存在的函数⊙﹏⊙ ...
- 学习练习 java 验证码练习
String str="1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM "; int b[]=new ...
- 洛谷P1983 车站分级
P1983 车站分级 297通过 1.1K提交 题目提供者该用户不存在 标签图论贪心NOIp普及组2013 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 求帮忙指出问题! 我这么和(diao ...
- EXE中释放文件
今天有个朋友问到VC能否释放多个EXE.DLL或WAV等文件,我便做了个实例给他. (注意:以下释放资源代码是不受文件扩展名所限制的,你可以释放更多类型文件) 下面是我写了个很方面的函数给大家用! 1 ...