数据结构练习 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 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 = 0; i < K; i++)
    {
        temp = 1;
        flag = true;
        for (j = 0; 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 0;
}

时间: 2024-07-31 07:55:17

数据结构练习 02-线性结构3. Pop Sequence (25)的相关文章

线性结构4 Pop Sequence

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, ..., Nand pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example,

数据结构专题 ——栈的应用 A1051.Pop Sequence(25)

解法1:(自己码的) #include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int MAXN = 10000; stack<int> stk; int main(){ int M,N,K; scanf("%d%d%d",&M,&N,&K); int temp[N]; bool flag; int

02-线性结构4 Pop Sequence (25 分)

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 pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example

1051. Pop Sequence (25)

1051. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

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

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

13. C#数据结构与算法 -- 线性结构

本文中,我们讨论了三个部分的内容: 什么是线性结构,线性结构有哪些特点 . 详细介绍了一个最简单线性结构顺序表,并且通过源代码进行一些的分析. 最后还举了一个例子,让我们更好的理解顺序表. 第一部分:什么是线性结构,线性结构有哪些特点 什么是线性结构,线性结构是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这 种一对一的关系指的是数据元素之间的位置关系,即: (1)除第一个位置的数据元素外,其它数据元素

Mooc数据结构-基础和线性结构

1 数据结构 解决问题方法的效率,跟数据的组织方式有关 解决问题方法的效率,跟空间的利用效率有关 解决问题方法的效率,跟算法的巧妙程度有关 数据结构 数据对象在计算机中的组织方式 逻辑结构 物理存储结构 数据对象必定与一系列加在其上的操作相关联 完成这些操作所用的方法就是算法 抽象数据类型(Abstract Data Type) 数据类型 数据对象集 数据集合相关联的操作集 抽象: 描述数据类型的方法不依赖与具体实现 与存放数据的机器无关 与数据存储的物理结构无关 与实现操作的算法和编程语言无关

02-线性结构4 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 ca