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. Direct3D-3 四元数

        其实本来这篇文章是打算接上篇的各种变化矩阵的推导了,想了想,还是先讲四元数吧.本人的文章并不会提到欧拉角,因为我自己没弄懂欧拉角的万向锁问题.     很多人学习数学时,会有这样一个疑惑,这东 ...

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

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

  3. [改善Java代码]使用构造块精炼程序

    建议36: 使用构造代码块精炼程序 什么叫代码块(Code Block)?用大括号把多行代码封装在一起,形成一个独立的数据体,实现特定算法的代码集合即为代码块,一般来说代码块是不能单独运行的,必须要有 ...

  4. 关于修改Eclipse工作空间对应的文件夹名称之后的处理.

    把文件夹名字从"xhkong"变成"xhkong(maintenance5.6)"之后打开这个工作空间. 导入git仓库我发现了一个之前没有发现的小技巧. 导入 ...

  5. C语言sprintf与sscanf函数[总结]

    sprintf函数 sprintf函数原型为 int sprintf(char *str, const char *format, ...).作用是格式化字符串,具体功能如下所示: (1)将数字变量转 ...

  6. SoftReference

    本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用    在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说 ...

  7. Ajax-数据格式-xml,json

    xml demo testDataXml <%@ page language="java" contentType="text/html; charset=UTF- ...

  8. Objective-C 【关于导入类(@class 和 #import的区别)】

    之前我们分析过 #include 和 #import 的区别,#import不会引起交叉编译,#import 确定一个文件只能被导入一次,使在递归包含中不会出现问题. 那么 #import 和 @cl ...

  9. JSON解析例子

    //解析的东西是数组就用数组接受,是字典就用字典接受 //my.h#ifndef __1_Header_h#define __1_Header_h#define DEBUG 1#define aa 1 ...

  10. SQLite的简单应用

    安装部署 1)进入 SQL 下载页面:http://www.sqlite.org/download.html 2)下载预编译二进制文件包. Windows 环境的如下: 下载完之后,就算部署完成.(P ...