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. Emacs 配置 Python 编程环境

    python编程环境设置涉及到:自动完成.语法检查.虚拟环境. 为了不把系统搞乱,在python的虚拟环境中安装相关的插件. 一.安装python虚拟环境 virtualenvwrapper sudo ...

  2. XML 格式转JSON 格式

    #import <Foundation/Foundation.h> #pragma GCC diagnostic push #pragma GCC diagnostic ignored & ...

  3. android开发之路07(无硝烟的战争)

    如何做一名优秀的android面试官? 如何做一名优秀的android候选者? 提到这个问题我不得不提起我们小升初,初升高,高生升本这几个历程中我们与出题人之间的无硝烟的战争.我们总是为自己的成绩担心 ...

  4. django–url

    当我们创建了一个项目时,系统为我们创建了一份urlconf, 它可能是这样的: urlpatterns = [ url(r'^admin/', admin.site.urls), ] r'^admin ...

  5. Linux文件与目录管理之ls的使用

    来源:鸟哥的私房菜 查看文件与目录 ls ls [-aAdfFhilnrRSt] 目录名 ls [--color={never,auto,always}] ls [--full-time] 目录名 选 ...

  6. 你所不知道的z-index的用法

    在开始今天的内容之前,先让我们来看下面一段代码: <style type="text/css"> #div1,#div2{ width:200px; height:20 ...

  7. 纯CSS手风琴效果

    CSS手风琴效果 主体代码如下:                                                                                     ...

  8. 从零单排Linux – 1 – 简单命令

    从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...

  9. css选择器有哪些?哪些属性可以继承?优先级算法如何计算?内联和important哪个优先

    CSS选择器:基本可以分为通配选择器,标签选择器,类选择器,ID选择器,简单属性选择,具体属性选择,根据部分属性值选择,特定属性选择,从结构上来分还有后代选择器,子元素选择器,相邻兄弟选择器以及伪类. ...

  10. ActiveMQ(5.10.0) - 删除闲置的队列或主题

    方法一 通过 ActiveMQ Web 控制台删除. 方法二 通过 Java 代码删除. ActiveMQConnection.destroyDestination(ActiveMQDestinati ...