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. UIButton 长按点击 背景改变效果

    1.添加按钮的点击事件和按下事件 [btn setImage:[UIImage imageNamed:@"NorMal.png"] forState:UIControlStateN ...

  2. php常用[字符串]函数

    nl2br 功能:化换行符为<br> <?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $ ...

  3. nrf51822裸机教程-RTC

    RTC0被协议栈使用了.所以在跑蓝牙程序的情况下.RTC0不能使用. RTC相关寄存器如下: EVTEN,EVTENSET,EVTENCLR. 这三个寄存器用来设置是否使能某个事件.(TICK,OVR ...

  4. Eclipse+Qt开发环境设置(Linux和Win)

    文章摘要: Windows,Linux平台下安装使用Eclipse + QT4.4.3开发环境 Windows,Linux新建project时的配置(不使用QT预置项目类型,而是手工配置) 使用Ecl ...

  5. C#远程共享文件路径访问

    public class Win32ServiceManager    {        private string strPath;        private ManagementClass ...

  6. C++线程池的实现(二)

    参考文章:http://blog.csdn.net/huyiyang2010/archive/2010/08/10/5801597.aspx // CThread.h #ifndef __MY_THR ...

  7. Linux学习之CentOS(十)--虚拟机下的CentOS如何上网

    原地址:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/05/3001148.html 这篇随笔应该说跟CentOS的学习关系不是很大, ...

  8. php--linux环境下的主从复制

    1.编辑数据库配置文件my.cnf,一般在/etc/目录下. #vi /etc/my.cnf 在[mysqld]的下面加入下面代码:[第一步查看本文件夹中代码是否已经存在,存在不需要进行添加] 只是修 ...

  9. ArcGIS API for Silverlight之配准JPG图片地图文字倾斜解决方案

    原文:ArcGIS API for Silverlight之配准JPG图片地图文字倾斜解决方案 根据实际JPG图片进行配准后,发布的地图,利用ArcGIS API for Silverlight在网页 ...

  10. 数据写入文本文件并读出到浏览器的PHP代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...