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. CSS完成三角形

    通过设置div的border属性,来实现! 将div的height和width属性分别设置为0px,将4边的border值设置为一样的宽度,通过调节边框的颜色课看到效果. <style> ...

  2. Python学习笔记 第一课 列表

    Python的列表就像是一个数组: 一.创建列表 movies=["The Holy Grail","Then Life of Brian","The ...

  3. android开发之路11(用SharedPreferences存储数据)

    Android平台给我们提供了一个SharedPreferences类,实际上SharedPreferences处理的就是一个key-value(键值对),它是 一个轻量级的存储类,特别适合用于保存软 ...

  4. jquery实现表格行的动态增加和删除

    $("#Addmaterial").click(function () {//Addmaterial是增加按钮的ID $("#tab tr").attr(&qu ...

  5. [改善Java代码]Java的泛型是类型擦除的

    泛型可以减少强制类型的转换,可规范集合的元素类型,还可以提高代码的安全性和可读性,正是因为有了这些优点,自从Java引入泛型之后,项目的编码规则上便多了一条,优先使用泛型. Java泛型(Generi ...

  6. poj 1987 树的分治

    思路:1741的A1送 1. #include<iostream> #include<cstring> #include<algorithm> #include&l ...

  7. 会话跟踪技术——Session

    一.什么是Session Session从用户访问页面开始,到断开与网站连接为止,形成一个会话的生命周期.在会话期间,分配客户唯一的一个SessionID,用来标识当前用户,与其他用户进行区分. Se ...

  8. 初识CSS3之媒体查询(2015年05月31日)

    一.什么是媒体查询 媒体查询是面向不同设备提供不同样式的一种实现方式,它可以为每种类型的用户提供最佳的体验,也是响应式设计的实现方式. 现今每天都有更多的手机和平板电脑问市.消费者能够拥有可想象到的各 ...

  9. unity3d游戏无法部署到windows phone8手机上的解决方法

    今天搞了个unity3d游戏,准备部署到自己的lumia 920上,数据线连接正常,操作正常,但是“build”以后,始终无法部署到手机上,也没有在选择的目录下生产任何相关文件.(你的系统必须是win ...

  10. fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    为完成老师的任务,用c++写个计算器,又去学点c++,结果就有各种问题,如题 http://www.cnblogs.com/newpanderking/articles/3372969.html 感谢 ...