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. 20145235《Java程序设计》第7周学习总结

    教材学习内容总结 13.1 认识时间与日期 格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度不一,本身自传减速而造成误差. 世界时(UT):通过观测远方星体跨过子午线而得,受地 ...

  2. 256 terabytes random-access memory

    Computer Systems A Programmer's Perspective Second Edition As we will discuss, the extension of IA32 ...

  3. Java 获取类名,函数名,行数

    C++下用宏来实现.分别是__FILE__,__func__,__LINE__分别代表,C++编译自动在每个文件中设定__FILE__类型是字符串常量 ,将__LINE__替换为当前行数,类型是数字常 ...

  4. C++ 简易时间类

    .h file #ifndef LIBFRAME_DATETIME_H_ #define LIBFRAME_DATETIME_H_ #include <stdint.h> #include ...

  5. 设计模式:中介者模式(Mediator)

    定   义:用一个中介对象来封装一系列对象的交互.中介者使各个对象不需要显示地相互作用,从而耦合松散,而且可以独立的改变他们之间的交互. 结构图: Mediator类,抽象中介者类 abstract ...

  6. iOS开发教程之:iPhone开发环境搭建

    安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...

  7. 闭包 Clousure

    闭包 Clousure 参考:http://caibaojian.com/javascript-closures.html?fid=776%230-tsina-1-25974-397232819ff9 ...

  8. 20145211 《Java程序设计》实验报告三:敏捷开发与XP实践

    实验内容 使用 git上传代码 使用 git相互更改代码 实现代码的重载 XP基础 XP核心实践 相关工具 一.git上传代码 这一部分是与我的partner合作的,详见他的博客- 20145326蔡 ...

  9. json的eval为什么要用msg.d

    在做一个关于搜索功能时用到了jquery autocomplete,发现返回数据时都用到了一个.d,比如: var datas = eval('(' + msg.d + ')'); 这个.d是什么呢, ...

  10. Aptana Studio 3的汉化

    Aptana Studio 3(下面简称Aptana 3)的汉化方法 1.找到这个网站 http://aptana.com/support 2.单击下面的链接 view documentation 在 ...