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. #include<iostream>
  2. #include<stack>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int M; //maximum capacity of the stack
  8. int N; //the length of push sequence
  9. int K; //the number of pop sequence to be checked
  10. cin >> M >> N >> K;
  11. int i, j;
  12. int input, temp;
  13. bool flag = true;
  14. stack<int> sta;
  15.  
  16. for (i = ; i < K; i++)
  17. {
  18. temp = ;
  19. flag = true;
  20. for (j = ; j < N; j++)
  21. {
  22. cin >> input;
  23. while (sta.size() <= M && flag)
  24. {
  25. if (sta.empty() || sta.top() != input)
  26. {
  27. sta.push(temp++);
  28. }
  29. else if (sta.top() == input)
  30. {
  31. sta.pop();
  32. break;
  33. }
  34. }
  35. if (sta.size() > M)
  36. {
  37. flag = false;
  38. }
  39. }
  40.  
  41. if (flag)
  42. cout << "YES" << endl;
  43. else
  44. cout << "NO" << endl;
  45. while (!sta.empty())
  46. sta.pop();
  47. }
  48. return ;
  49. }

数据结构练习 02-线性结构3. Pop Sequence (25)的更多相关文章

  1. pat02-线性结构4. Pop Sequence (25)

    02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  2. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

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

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

  5. 浙大数据结构课后习题 练习二 7-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 ...

  6. 02-线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...

  7. Java数据结构介绍(线性结构和非线性结构)

    数据结构包括:线性结构和非线性结构. 线性结构 数据元素之间存在一对一的线性关系 包括顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的 链式存储的线性表称为链表,链表 ...

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

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

随机推荐

  1. 系统调用表 linux 2.6.32

    [root@localhost log]# find / |grep syscall_table /usr/src/kernels/linux-/arch/x86/kernel/syscall_tab ...

  2. iOS开发UI篇-tableView在编辑状态下的批量操作(多选)

    先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @pr ...

  3. 移动终端学习1:css3 Media Queries简介

    移动终端学习之1:css3 Media Queries简介 1.简介 这篇文章写的不错,我就不重复了,来个链接:http://www.w3cplus.com/content/css3-media-qu ...

  4. 深入理解计算机系统第二版习题解答CSAPP 2.9

    基于三元色R(红)G(绿)B(蓝)关闭(0)和打开(1),能够创建8种不同的颜色,如下: R G B 颜色 R G B 颜色 0 0 0 黑色 1 0 0 红色 0 0 1 蓝色 1 0 1 红紫色 ...

  5. mono & apache install

    1.red hat 6安装完后网卡是默认不启动的 作为双生兄弟的CENTOS同样如是 第一步 设置网卡开机启动 进入 路径此目录下修改网卡配置文件 如果网卡驱动正常 会有如下文件 只要修改 ifcfg ...

  6. 《转》手把手教你使用Git

    Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不 需要联网了,因为版本都是在自己的电脑上.既然每个人的电脑都有一个完整的版本库,那多个人如何 ...

  7. 推荐几个对Asp.Net开发者比较实用的工具 2

    推荐几个对Asp.Net开发者比较实用的工具.大家有相关工具也可以在评论区留言,一起努力学习. 作为程序员要有挑战精神,大家可以尝试一下这些工具. 已经有篇文章写到了vs的扩展工具,这里不再累赘,请查 ...

  8. MySQL基本查询语句

    创建一张表 create table user ( id ) not null, name ) not null, birthDate date not null, gender ) not null ...

  9. C#垃圾回收机制详解

    一.托管代码/非托管代码 C#代码通过C#编译器编译成程序集,程序集由微软中间语言组成,CLR会为程序集开辟一个应用程序域,程序集就是运行在这个应用程序域里面的,应用程序域是相互独立的,互不影响. 托 ...

  10. Js 学习资料

    Js 语法 http://www.php100.com/manual/jquery/   jqGrid控件 http://www.trirand.com/blog/jqgrid/jqgrid.html