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
#include<iostream>
#include<stack>
using namespace std; int main()
{
int M; //maximum capacity of the stack
int N; //the length of push sequence
int K; //the number of pop sequence to be checked
cin >> M >> N >> K;
int i, j;
int input, temp;
bool flag = true;
stack<int> sta; for (i = ; i < K; i++)
{
temp = ;
flag = true;
for (j = ; j < N; j++)
{
cin >> input;
while (sta.size() <= M && flag)
{
if (sta.empty() || sta.top() != input)
{
sta.push(temp++);
}
else if (sta.top() == input)
{
sta.pop();
break;
}
}
if (sta.size() > M)
{
flag = false;
}
} if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
while (!sta.empty())
sta.pop();
}
return ;
}

数据结构练习 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. C#隐式类型

    隐式类型 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  2. Java基础知识强化之IO流笔记62:三种方式实现键盘录入

    1. 三种方式实现键盘录入     System.in 标准输入流.是从键盘获取数据的 键盘录入数据三种方式:  A:main方法的args接收参数.  java HelloWorld hello w ...

  3. 基于DOM的XSS注入漏洞简单解析

    基于DOM的XSS注入漏洞简单解析http://automationqa.com/forum.php?mod=viewthread&tid=2956&fromuid=21

  4. Yii::app()方法详解

    Yii::app()方法详解 1.Yii::app()返回的是一个当前框架应用程序的对象 2.Yii::app()是framework/web/CWebApplication.php类实例化的对象,所 ...

  5. Css3炫酷总结使用

    先从CSS3的选择器说起: E F:所有的子孙元素: E>F: E中的子元素: E+F:E元素之后的最近的选择器: E~F:E中所以后面兄弟元素(CSS3 不包括自己本身,前面也不包括) att ...

  6. JQ判断按钮,复选框是否选中

    var oRdoValue=$("#Radio1").is (":checked")?"男":"女"; //获取单选框按 ...

  7. 插入排序算法--直接插入算法,折半排序算法,希尔排序算法(C#实现)

    插入排序算法主要分为:直接插入算法,折半排序算法(二分插入算法),希尔排序算法,后两种是直接插入算法的改良.因此直接插入算法是基础,这里先进行直接插入算法的分析与编码. 直接插入算法的排序思想:假设有 ...

  8. Power Map 更新日志

    2015-05-18,五月更新 Custom Regions feature,允许用户自定义区域要素,支持kml和shape格式 New customization features,包括图例/文本框 ...

  9. Xcode8 Could not build Objective-C module 'FBSDKCoreKit'

    解决方法是: 删除/Users/Rinpe/Library/Developer/Xcode/DerivedData下对应的文件夹即可.

  10. java 环境变量配置

    1.我的电脑 2.属性 3.高级 4.环境变量 5.新建 6.JAVA_HOME: C:\Program Files (x86)\Java 7.CLASSPATH: .;%JAVA_HOME%\;%J ...