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 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:

  1. 5 7 5
  2. 1 2 3 4 5 6 7
  3. 3 2 1 7 5 6 4
  4. 7 6 5 4 3 2 1
  5. 5 6 4 3 7 2 1
  6. 1 7 6 5 4 3 2

Sample Output:

  1. YES
  2. NO
  3. NO
  4. YES
  5. NO

题意:

给定一个有固定容量的栈,1,2,...,n是入栈序列,元素出栈顺序随意,现给定出栈顺序(e.g.1~n的一个排列),问这个出栈顺序是否合理,合理输出"YES",否则输出"NO"。

题解:

开一个队列,开一个栈,输入一个数,就队列中这个数及之前的数放入栈中,放入不能超过容量。然后看栈顶元素是不是这个数,是就下一个,不是就标记NO。

AC代码:

  1. #include<iostream>
  2. #include<stack>
  3. #include<queue>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<string>
  8. #include<cstring>
  9. using namespace std;
  10. int n,m,k;
  11. stack<int>s;
  12. queue<int>q;
  13. int main(){
  14. cin>>n>>m>>k;
  15. while(k--){
  16. while(!s.empty()) s.pop();
  17. while(!q.empty()) q.pop();
  18. int f=;
  19. for(int i=;i<=m;i++) q.push(i);
  20. for(int i=;i<=m;i++){
  21. int x;
  22. cin>>x;
  23. if(s.empty()||s.top()!=x){
  24. while(!q.empty()){
  25. if(s.size()<n) {
  26. //cout<<"把"<<q.front()<<"放栈"<<endl;
  27. s.push(q.front());
  28. q.pop();
  29. }
  30. else break;
  31. if(s.top()==x) break;
  32. }
  33. }
  34. if(s.top()==x){
  35. s.pop();
  36. //cout<<x<<"踢出栈"<<endl;
  37. continue;
  38. }
  39. f=;
  40. }
  41. if(f) cout<<"YES"<<endl;
  42. else cout<<"NO"<<endl;
  43. }
  44. return ;
  45. }

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)的更多相关文章

  1. 1051 Pop Sequence (25分)栈

    刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...

  2. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  3. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  4. 【PAT】1051 Pop Sequence (25)(25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  5. 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 ...

  6. 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 p ...

  7. PAT 甲级 1051 Pop Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...

  8. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  9. 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 ...

随机推荐

  1. css定位篇

    1.浮动的特性: 1.浮动的元素脱标 2.浮动的元素互相贴靠 3.浮动的元素有“字围”效果 4.收缩的效果 前提是标准文档流,margin的垂直方向会出现塌陷问题. 如果盒子居中:margin:0 a ...

  2. Java stackoverflowerror异常与outofmemoryerror异常区别

    1.stackoverflow: 每当java程序启动一个新的线程时,java虚拟机会为他分配一个栈,java栈以帧为单位保持线程运行状态:当线程调用一个方法是,jvm压入一个新的栈帧到这个线程的栈中 ...

  3. [转]对于BIO/NIO/AIO,你还只停留在烧开水的水平吗

    原文:https://www.javazhiyin.com/40106.html https://coding.imooc.com/class/381.html ------------------- ...

  4. 前端笔记-bom

    BOM对象 BOM即浏览器对象模型,它与dom不同的是可以操作浏览器窗口,使用它的接口我们可以改变窗口,状态栏,文本,及其他与除页面以外其他动作,使得js可以和我们浏览器进行沟通 窗口 即window ...

  5. chrome开启headless模式以及代理

    google-chrome-stable --disable-gpu --remote-debugging-port=9222 --headless -remote-debugging-address ...

  6. hadoop各版本下载地址

    http://mirror.bit.edu.cn/apache/hadoop/common/

  7. 关于Maven打包

    Maven打包构建完全指南和最佳实践 Maven最佳实践:划分模块 IDEA一个项目引用另一个项目 IDEA创建多个模块MavenSpringBoot项目   这个简单明了,基础知识

  8. Elasticsearch-head使用及ES相关概念

    elasticsearch-head安装和介绍已在上一篇讲了. 在浏览器访问http://localhost:9100,可看到如下界面,表示启动成功: 仔细观察,我们会发现客户端默认连接的是我们ela ...

  9. 18.4.09 模拟考 zhx P75

    题目链接 https://files.cnblogs.com/files/lovewhy/P75.pdf P75 竞赛时间: ????年??月??日??:??-??:?? 注意事项(请务必仔细阅读) ...

  10. AWS API Gateway Swagger定义

    导出Swagger接口定义文件 在AWS API Gateway界面上,可以导出swagger接口定义文件. 而后利用Node js swagger-ui 依赖,生成swagger接口地址 Cloud ...