题目


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

基本思路

先把输入的数列用数组保存,然后依次将1,2,3...N入栈,每次入栈判断栈顶是否与数组中当前要输出的元素相等,如果相等则出栈(循环)。如果遇到栈满或无法输出数组所有元素的情况则输出No。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
template < class T >
class AStack {
private:
    int size; // 数组的规模
    T * Array; // 存放堆栈元素的数组
    int top; // 栈顶所在数组元素的下标
public:
    AStack(int MaxStackSize) // 构造函数
    {
        size = MaxStackSize; Array = new T[MaxStackSize]; top = -1;
    }
    ~AStack() { delete[] Array; } // 析构函数
    bool Push(const T& item); // 向栈顶压入一个元素
    bool Pop(T & item); // 从栈顶弹出一个元素
    T Top() const { return Array[top]; } // 查看栈顶元素
    int IsEmpty(void) const { return top == -1; }
    // 检测栈是否为空
    int IsFull(void) const { return top == size - 1; }
    // 检测栈是否为满
    void clear(void) { top == -1; } // 清空栈
};

template < class T >
bool AStack<T>::Push(const T& item)
{
    if (this->IsFull())
    {
        //    cout << "full" << endl;
        return false;
    }
    else
    {
        Array[top + 1] = item;
        top++;
    }
    return true;
}

template < class T >
bool AStack<T>::Pop(T& item)
{
    if (this->IsEmpty())
    {
        //cout << "empty" << endl;
        return false;
    }
    else
    {
        item = Array[top];
        top--;
    }
    return true;
}

int Judge(int M, int N);
int main()
{
    int M, N, K;

    cin >> M;
    cin >> N;
    cin >> K;
    int *yes = new int[K];

    for (int i = 0; i < K; i++)
    {
        yes[i] = Judge(M, N);
    }
    for (int i = 0; i < K; i++)
    {
        if (yes[i])
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

int Judge(int M, int N)
{
    int item[1000], now = 0;
    AStack<int> stack(M);
    for (int i = 0; i < N; i++)
    {
        cin >> item[i];
    }
    for (int i = 1; i <= N; i++)
    {
        stack.Push(i);

        while (item[now] == stack.Top())
        {
            int temp;
            stack.Pop(temp);
            now++;
            if (now == N)
            {
                //cout << "YES" << endl;
                return 1;
                break;
            }
            continue;
        }
        if (stack.IsFull())
        {
            //cout << "NO" << endl;
            return 0;
            break;
        }
    }
    return 0;
}

总结

试了下,好像在读输入的时候同时输出也是可以的。

02-线性结构4 Pop Sequence的更多相关文章

  1. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

  2. pat02-线性结构4. Pop Sequence (25)

    02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  3. 02-线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...

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

  5. 数据结构练习 02-线性结构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 (25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

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

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

  9. [刷题] PTA 02-线性结构4 Pop Sequence

    模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...

随机推荐

  1. FreeType in OpenCASCADE

    FreeType in OpenCASCADE eryar@163.com Abstract. FreeType is required for text display in the 3D view ...

  2. PyCharm 2017 免费 破解 注册 激活 教程(附 License Server 地址)(Python 编辑器 IDE 推荐)

    许多朋友都在问如何破解 PyCharm 2017 Professional 专业版,咪博士对此是坚决反对的! 不到万不得已,请不要这样做.破解之前,请拖到文章末尾,思考几个问题,想明白你确实需要这样做 ...

  3. 【20171025早】alert(1) to win 练习

    本人黑绝楼,自称老黑,男,25岁,曾经在BAT工作过两年,但是一直都是底层人员,整天做重复性工作,甚敢无趣,曾和工作十年之久的同事聊天,发现对方回首过往,生活是寡淡如水,只有机械性工作.旋即老黑毅然决 ...

  4. CentOS7.3安装NVIDIA-1080ti驱动、cuda、cudnn、TensorFlow

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 Ubuntu非要换centOS...好吧... 看了很多是通过ELRepo源安装驱动,不过我没 ...

  5. 扩展Spring切面功能

    概述 Spring的切面(Spring动态代理)在Spring中应用十分广泛,例如还有事务管理,重试等等.网上介绍SpringAop源码很多,这里假设你对SpringAop有基本的了解.如果你认为Sp ...

  6. 在Javaava中stringBuilder的用法

    String对象是不可改变的.每次使用 System.String类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新的  ...

  7. 读书笔记-你不知道的JS中-函数生成器

    这个坑比较深 可能写完我也看不懂(逃 ES6提供了一个新的函数特性,名字叫Generator,一开始看到,第一反应是函数指针?然而并不是,只是一个新的语法. 入门 简单来说,用法如下: functio ...

  8. HDU1754 I hate it(线段树 单点修改)

    好久没打线段树,来一道练练手,但说句实话,I really hate it!!!!   很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.  不管 ...

  9. FatMouse and Cheese

    FatMouse and Cheese Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  10. PHP读取数据库表显示到前台

    <?php$username=$_GET['uid']; //获取一个值作为查询条件 $result=$db->query("select * from trip where a ...