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 (栈)的更多相关文章

  1. PAT 1051 Pop Sequence[栈][难]

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

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

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

  4. 1051. Pop Sequence

    原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...

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

  6. Pop Sequence

    题目来源:PTA02-线性结构3 Pop Sequence   (25分) Question:Given a stack which can keep M numbers at most. Push ...

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

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

  9. PAT1051:Pop Sequence

    1051. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

随机推荐

  1. Python学习笔记 第二课 循环

    >>> movies=["The Holy Grail", 1975, "The Life of Brian", 1979, "Th ...

  2. PHP用反撇号(`,也就是键盘上ESC键下面的那个,和~在同一个上面)执行外部命令

    例如: echo `whoami`; // 导出数据库,要导入的文件夹必须要有可写权限, -u -p之后的内容必须要紧挨着写 echo `mysqldump -h localhost -u$DbUse ...

  3. Url几个常用的函数

    parse_url() 本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分. 本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分.不完整的 URL ...

  4. VC操作Excel之基本操作

    // 变量的定义 _Application app; Workbooks books; _Workbook book; Worksheets sheets; _Worksheet sheet; Ran ...

  5. 【欧拉定理】计算(a^(b^c))%1000000007

    欧拉定理(称费马-欧拉定理或欧拉 函数定理) 欧拉定理表明,若n,a为正整数,且n,a互素(即gcd(a,n)=1),则 这个定理可以用来简化幂的模运算.比如计算7222的个位数,实际是求7222被1 ...

  6. linux nginx启动 重启 关闭命令

    启动操作 nginx -c /usr/local/nginx/conf/nginx.conf -c参数指定了要加载的nginx配置文件路径 停止操作停止操作是通过向nginx进程发送信号来进行的 步骤 ...

  7. Android Fragment 实例

    Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍.Android Fragment使用.Android FragmentManage F ...

  8. 转: utf16编码格式(unicode与utf16联系)

    转自: http://www.cnblogs.com/dragon2012/p/5020259.html UTF-16是Unicode字符集的一种转换方式,即把Unicode的码位转换为16比特长的码 ...

  9. CSS3秘笈第三版涵盖HTML5学习笔记6~8章

    第二部分----CSS实用技术 第6章,文本格式化 指定备用字体: font-family:Arial,Helvetica,sans-serif; 当访问者没有安装第一种字体时,浏览器会在列表中继续往 ...

  10. spring分布式事务学习笔记

    最近项目中使用了分布式事务,本文及接下来两篇文章总结一下在项目中学到的知识. 分布式事务对性能有一定的影响,所以不是最佳的解决方案,能通过设计避免最好尽量避免. 分布式事务(Distributed t ...