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

题目描述:

一列数, 只能以1, 2, …., N的push 到stack里面, 但是可以再任意时刻pop出一个数字, 问给定一个序列, 是不是一个可行的按照这样的规则可以出现的一个pop序列.

算法分析:

思路:总结规律,使用hash

使用hash记录已经弹出栈的数字

从前向后扫,两种情况下是不可能的

1、比如当前扫到了7,查看1~6是否已经弹出,若没有弹出的数量count<M(栈的容量),则说明不可能。

2、比如当前扫到了7,而前一个是4,就要检测5和6是否弹出,如果有一个没弹出,则不可能

注意点:

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream> using namespace std; int hashs[];
int M, N, K; int getScale(int x){
int cnt = ;
for (int i=; i<=x; i++) {
if (hashs[i] == )
cnt++;
}
return cnt;
}
bool ckinv(int x,int y){
for (int i=y+; i<x; i++){
if (hashs[i] == )
return false;
}
return true;
} int main()
{
scanf("%d%d%d", &M, &N, &K);
for (int i=; i<K; i++) {
memset(hashs, , sizeof(hashs));
int a[N+];
for (int j=; j<=N;j++) {
scanf("%d", a+j);
}
int pre;
bool flag = true;
for (int j=; j<=N; j++) {
int tmp = a[j];
if (getScale(tmp) > M) {
printf("NO\n");
flag = false;
break;
}
if (j!= && ckinv(pre, tmp)== false) {
printf("NO\n");
flag = false;
break;
}
hashs[tmp] = ;
pre = tmp;
}
if (flag == true) printf("YES\n");
}
return ;
}

PAT 解题报告 1051. Pop Sequence (25)的更多相关文章

  1. PAT (Advanced Level) 1051. Pop Sequence (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

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

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

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

  4. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

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

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

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

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

  9. PAT甲题题解-1051. Pop Sequence (25)-堆栈

    将1~n压入最多为m元素的栈 给出k个出栈序列,问你是否能够实现. 能输出YES 否则NO 模拟一遍即可,水题. #include <iostream> #include <cstd ...

随机推荐

  1. 专治XP正在启动就蓝屏重启一直循环

    我5月6号发帖求助,很多同行都要说换系统.PE修复启动项等,这些早在我入行电脑行业10年前都会的.但是客户的数据是用金钱也买不到的,不能就这样给换了吧,这样也让客户小看我们搞电脑行业的了. 好说正题: ...

  2. 免费手机号码归属地API查询接口和PHP使用实例分享

    免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...

  3. NSQ部署

    一.      简介 NSQ主要有三个主要程序和一个Web服务程序: nsqd:是守护进程,接收,缓存,并投递消息给客户端 nsqlookupd:是一个守护进程,为消费者提供运行时发现服务,来查找指定 ...

  4. 在Delphi中如何动态创建dbf数据库(一)?

    table2.Close; table2.Active:=false; table2.Exclusive:=true; table2.TableName:='h:\gzkd\sds'; table2. ...

  5. 浅析C++的内存管理

    在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/ 静态存储区和常量存储区. 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参 ...

  6. 【转】CSS(10)盒子模型

    CSS中, Box Model叫盒子模型(或框模型),Box Model规定了元素框处理元素内容(element content).内边距(padding).边框(border) 和 外边距(marg ...

  7. go liteIDE

    go  liteIDE 1 COMM FILE package pricetable import ( "fmt" "math" "os" ...

  8. 详解linux系统的启动过程及系统初始化

    一.linux系统的启动流程 关于linux系统的启动流程我们可以按步进行划分为如下: POST加电自检 -->BIOS(Boot Sequence)-->加载对应引导上的MBR(boot ...

  9. docker learning

    Docker 配置文件位置 Docker 的配置文件可以设置大部分的后台进程参数,在各个操作系统中的存放位置不一致 在 ubuntu 中的位置是:/etc/default/docker 在 cento ...

  10. 关于HBase的概述

    1.hbase的特点 ->数据存储量可以达到亿级别数据维持在秒级 ->按列存储的数据库 ->能够存储上百万列 ->hbase的底层存储依赖于HDFS ->如何扩展hbas ...