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<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main(){
int N, M, K, temp;
stack<int> stk;
scanf("%d%d%d", &M, &N, &K);
for(int i = ; i < K; i++){
int index = , find = ;
for(int j = ; j < N; j++){
scanf("%d", &temp);
if(stk.empty()){
stk.push(index++);
}
if(stk.top() < temp){
while(stk.top() < temp && stk.size() < M){
stk.push(index++);
}
if(stk.top() < temp)
find = ;
}
if(stk.top() > temp){
find = ;
continue;
}
if(stk.top() == temp){
stk.pop();
continue;
}
}
if(find == )
printf("NO\n");
else printf("YES\n");
while(!stk.empty()){
stk.pop();
}
}
cin >> N;
return ;
}

总结:

1、栈的模拟题。注意栈的大小有限制。

2、注意每次使用完毕后清空stack。

A1051. Pop Sequence的更多相关文章

  1. PAT甲级——A1051 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 ...

  2. 1051. Pop Sequence

    原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...

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

  4. Pop Sequence

    题目来源:PTA02-线性结构3 Pop Sequence   (25分) Question:Given a stack which can keep M numbers at most. Push ...

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

  6. Pop Sequence (栈)

     Pop Sequence (栈) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...

  7. 数据结构练习 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 ...

  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. Jmeter(三十三)_JsonPath表达式提取响应

    我们在用jmeter做接口测试的时候,有的时候会遇到一些复杂的json响应.比如多层list嵌套时的取值 一个简单的例子: $..Name:列出所有省份 $..Province[0].Name 提取P ...

  2. 轮廓(Outline) 实例

    1.在元素周围画线本例演示使用outline属性在元素周围画一条线. <style type="text/css"> p{border:red solid thin;o ...

  3. Linux系统下本地yum镜像源环境部署-完整记录

    之前介绍了Linux环境下本地yum源配置方法,不过这个是最简单最基础的配置,在yum安装的时候可能有些软件包不够齐全,下面说下完整yun镜像源系统环境部署记录(yum源更新脚本下载地址:https: ...

  4. B. Equations of Mathematical Magic

    思路 打表找规律,发现结果是,2的(a二进制位为1总数)次方 代码 #include<bits/stdc++.h> using namespace std; #define ll long ...

  5. 查看电脑保存的wifi密码

    查看电脑保存的wifi密码 查看电脑链接过的WiFinetsh wlan show profile 查看wifi的密码netsh wlan show profile name=8888 key=cle ...

  6. 申港集中运营平台Linux测试环境架构搭建

    架构图 服务一览 ESB安装 ESB全称为Enterprise Service Bus,即企业服务总线.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素.ESB的出现改变了传统的软件架 ...

  7. 文本文件合并(C++实现)

    直接附上代码吧 #include<iostream> #include<fstream> #include<cstdlib> using namespace std ...

  8. Why yarn

    http://www.cnblogs.com/LeftNotEasy/archive/2012/02/18/why-yarn.html https://www.ibm.com/developerwor ...

  9. 记Git报错-refusing to merge unrelated histories

    记Git报错-refusing to merge unrelated histories   系统:win7 git版本: 2.16.2.windows.1 问题 1.本地初始化了git仓库,放了一些 ...

  10. [转帖]看完这篇文章你还敢说你懂JVM吗?

    看完这篇文章你还敢说你懂JVM吗? 在一些物理内存为8g的服务器上,主要运行一个Java服务,系统内存分配如下:Java服务的JVM堆大小设置为6g,一个监控进程占用大约 600m,Linux自身使用 ...