线性结构3 Pop Sequence(PAT)
来源:互联网 发布:java含金量高的证书 编辑:程序博客网 时间:2024/06/08 13:49
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, ifM 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 2
Sample Output:
YESNONOYESNO
题意
第一行输入 M, N,T
M代表栈的容量,N为序列长度,T为测试组数
我们可以向容量为M的栈中按照1 , 2, 3..的顺序推入 1 ,2, 3.. , 可以任意弹出。问能否输出给定序列。
样例 5 6 4 3 7 2 1
推入 1 2 3 4 5 , 弹出 5, 此时 1 2 3 4
输入 6 ,推入 6, 此时 1 2 3 4 6 , 弹出6 ,此时 1 2 3 4
输入 4 ,弹出 4,此时 1 2 3
输入 3 ,弹出 3,此时 1 2
输入 7,推入 7 , 此时 1 2 7,弹出 7,此时 1 2
输入 2, 弹出 2 , 此时 1
输入1 ,弹出 1. 输出 YES!
直接模拟题意解决
#include<iostream>#include<cstdio>#include<stack>using namespace std;int main(){int n,m,t;scanf("%d%d%d",&n,&m,&t);stack<int>st;for(int i = 0;i<t;i++){int tmp = 1;bool False = 0;for(int j=0;j<m;j++){int input ;scanf("%d",&input);while(st.size() <= n && !False){ //满足条件就不断推入 1,2,3...if(st.empty() || st.top() != input)st.push(tmp++);else if(st.top() == input){ //如果栈顶元素与输入的一样了,就弹出。st.pop(); break;}if(st.size() > n){False = 1; break; //超过栈的范围了}}}if(!False)printf("YES\n");elseprintf("NO\n");while(!st.empty())st.pop();}return 0;}
- 线性结构3 Pop Sequence(PAT)
- 数据结构 PAT 02-线性结构3 Pop Sequence
- PAT 02-线性结构3. Pop Sequence (25)
- PAT 数据结构 02-线性结构4. Pop Sequence (25)
- PAT 02-线性结构4 Pop Sequence【JAVA实现】
- PAT数据结构_02-线性结构4 Pop Sequence (25分)
- PTA 02-线性结构3 Pop Sequence (使用STL)
- 02-线性结构3 Pop Sequence
- 02-线性结构3 Pop Sequence
- 02-线性结构3 Pop Sequence
- 02-线性结构3 Pop Sequence
- 02-线性结构3 Pop Sequence
- 线性结构4 Pop Sequence
- 02-线性结构3 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分) (栈)
- 02-线性结构4 Pop Sequence(25 分)
- 02-线性结构3. Pop Sequence
- 02-线性结构3. Pop Sequence (25)
- [urls.py设置技巧]URLconf处理其一:URL模式
- Java中Static作用
- spacemacs快速入门
- 声控开关电路原理图-门电路、限流电阻应用经典分析
- Code First Migrations更新数据库结构的具体步骤
- 线性结构3 Pop Sequence(PAT)
- linux下core文件调试方法
- 工作总结
- XMPP框架 微信项目开发之网络通信基础——OSI_TCP/IP 参考模型的理解
- LightOJ - 1209 Strange Voting(二分图)
- 圆角 图形四周加边框
- SSH 不能连接的问题解决
- Criteria 外键匹配查询
- 黑马程序员--异常机制