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

题目分析:一道模拟题 写了半天
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
stack<int> S1; int main()
{
int M, N, K;
cin >> M >> N >> K;
vector<int> Array(N+);
vector<int> Test(N+);
for (int i = ; i <= N; i++)
Array[i] = i;
for (int i = ; i < K; i++)
{
while (S1.size())
S1.pop();
for (int j = ; j <= N; j++)
cin >> Test[j];
int k = , t = ,flag=;
while (t<=N)
{
if (S1.size()> M)
{
flag = ;
break;
}
else if (S1.size())
{
if (S1.top() == Test[t])
{
S1.pop();
t++;
}
else if(k<=N)
{
S1.push(Array[k]);
k++;
}
else
{
if (S1.top() == Test[t])
{
S1.pop();
t++;
}
else
{
flag = ;
break;
}
}
}
else {
S1.push(Array[k]);
k++;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}

1051 Pop Sequence (25分)的更多相关文章

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

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

  3. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  4. 1051 Pop Sequence (25分)栈

    刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...

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

  7. 【PAT】1051 Pop Sequence (25)(25 分)

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

随机推荐

  1. Distance function for sqlite

    Distance function for sqlite Posted on October 25, 2008 by Dave As part of an iPhone SDK project, I ...

  2. optimizing Wi-Fi solution for International School

    https://aweisoft.azurewebsites.net/Knowledge/Cisco/OptimizeWiFi/OptimizeWiFi.aspx Connect me on Link ...

  3. docker 技术全面整理

    docker 和 vm 虚拟机技术比较像,但又有一些区别. vm 像真机一样有 BIOS ,有硬盘,有网卡,声卡,可以安装操作系统, win7 win10 macOS ubuntu centOS,有好 ...

  4. MDI设置父子窗体

    1.新建父窗体,设置窗体属性:IsMdicontainer设置成true; 2.拖入button控件,修改button中的text. 3.点击button控件设置代码: //1.窗体实例化 Form ...

  5. 用 jQuery 实现表单验证(摘抄)——选自《锋利的jQuery》(第2版)第5章的例题 5.1.5 表单验证

    5.1.5 表单验证 表单(form)作为 HTML 最重要的一个组成部分,几乎在每个网页上都有体现,例如用户提交信息.用户反馈信息和用户查询信息等,因此它是网站管理者与浏览者之间沟通的桥梁.在表单中 ...

  6. list的常用方法

    注:lst为定义的列表变量,如:lst = [1, 2, 3, 4, 5] 增: 1.  lst.append()  末尾追加 lst = [1, 2, 3, 4, 5, 6] lst.append( ...

  7. 【Weiss】【第03章】练习3.6:有序多项式相加

    [练习3.6] 编写将两个多项式相加的函数.不要毁坏输入数据.用一个链表实现. 如果这两个多项式分别有M项和N项,那么你程序的时间复杂度是多少? 两个按幂次升序的多项式链表,分别维护一个指针. 幂较小 ...

  8. JavaScript每日学习日记(1)

    8.11.2019 1. lastIndexOf() 方法从尾到头进行检索. 2. 有三种提取部分字符串的方法: 2.1 slice(start, end)  如果某个参数为负,则从字符串的结尾开始计 ...

  9. Jmeter Agent自动化

    1.打开菜单栏-附件-系统工具-任务计划程序,新建PerformanceTest目录. 2.在PerformanceTest目录下新建一个基本任务. 3.完成. 这样,当我们在使用Jmeter进行分布 ...

  10. [剑指offer]6.从尾到头打印链表+18.删除链表节点

    链表 6.从尾到头打印链表 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 方法一 迭代 创建空列表res,将链表值head.val依次存进res,返回翻转后的res 代码 cl ...