Pop Sequence (栈)
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
一个数要出栈,那么比这个数小的数一定要先进栈
设 tem 为进栈的数。如果 栈为空 或 出栈的数 不等于 栈顶数,那么 tem进栈 后 自增。如果 出栈的数 等于 栈顶数,则出栈。如果栈溢出,那就错误(可能是要出栈的数太大 或者是 要出栈的数 小于 此刻的栈顶元素 )
#include <iostream> #include <string> #include<vector> #include <stack> using namespace std; int main() { int i,j,m,k,n,x; while(cin>>m) { cin>>n>>k; for(i=0;i<k;i++) { bool is=true; stack<int> tt; int tem=1; for(j=0;j<n;j++) { cin>>x; while(tt.size()<=m && is) { if(tt.empty() || tt.top()!=x) tt.push(tem++); else if(tt.top()==x) { tt.pop(); break; } } if(tt.size() > m) { is=false; } } if(is) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; }
Pop Sequence (栈)的更多相关文章
- PAT 1051 Pop Sequence[栈][难]
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
- PAT Advanced 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 and ...
- 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 ...
- Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分) Question:Given a stack which can keep M numbers at most. Push ...
- 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 ...
- 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 ...
随机推荐
- Log4Net之初步了解
原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/log4net%E4%B9%8B%E5%88%9D%E6 ...
- 从医生看病和快餐店点餐理解Node.js的事件驱动
第一个例子是关于医生看病. 在美国去看医生,需要填写大量表格,比如保险.个人信息之类,传统的基于线程的系统(thread-based system),接待员叫到你,你需要在前台填写完成这些表格,你站着 ...
- JDBC的批量查询报告内存溢出解决方法
由于表中的数据过多(我的超过了50W+),查询select * from table ....报告内存溢出 Exception in thread "main" java.lang ...
- [改善Java代码]警惕自增的陷阱
建议7: 警惕自增的陷阱 老师就说:自增有两种形式,分别是i++和++i,i++表示的是先赋值后加1,++i是先加1后赋值,这样理解了很多年也没出现问题,直到遇到如下代码,我才怀疑我的理解是不是错了: ...
- 对微信小程序的看法,不吹不黑
今日微信小程序正式上线了,昨天也稍稍地研究了一下小程序的概念以及开发过程.简而言之,微信小程序就是一款在微信端的轻应用,其实在支付宝也有类似的功能,只不过支付宝没有开放接口,而微信开放了接口供开发者开 ...
- 【转】APP测试要点
APP测试的时候,建议让开发打好包APK和IPA安装包,测试人员自己安装应用,进行测试.在测试过程中需要注意的测试点如下: 1.安装和卸载 ●应用是否可以在IOS不同系统版本或android不同系统版 ...
- Linux 命令 - ps: 显示当前进程的快照
命令格式 ps [options] 实例 a) 查看所有的进程. huey@huey-K42JE:~$ ps aux | head USER PID %CPU %MEM VSZ RSS TTY STA ...
- sql常识-IN 操作符
IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...
- Javascript之换肤(未完待续)
这个项目我还没有完全写出来,先记录至此.感觉是方法不对,背景图片的切换方法有Problem.如若有一大神发现了我的文章,还望指导,吾将感激不尽.日后代码还会再钻研再改改. <head> & ...
- .Net 微信开发与微信支付
.NET https://github.com/JeffreySu/WeiXinMPSDK JAVA http://git.oschina.net/pyinjava/fastweixin ...