题目来源PTA02-线性结构3 Pop Sequence   (25分)

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


  Sample Output:

YES
NO
NO
YES
NO

  分析:此题考察栈的操作,入栈的顺序是1,2,3......,N。出栈序列以5 6 4 3 7 2 1为例,要pop 5,就必须先push 1, push 2, push 3, push 4, push5, 此时栈顶元素为5,刚好匹配,才能进行pop操作。这里首先清空栈,设置一个将要入栈的顺序值temp,由1开始自增。当栈顶元素与读取的出栈序列值不匹配(还要考虑栈空的情况)时就进行入栈操作: Sta.push(temp++); ;当栈顶元素与读取的出栈序列值匹配,要进行出栈操作 Sta.pop(); 弹出栈顶元素,再读取下一个出栈序列值。如果栈中的元素个数超过了M,则说明出现了错误,这种出栈序列是不成立的。

  源码

#include<iostream>
#include<stack> //调用C++ STL中的堆栈容器
using namespace std; int main()
{
int M, N, K;
int obtain, pop; // obtain为将要入栈的顺序值(1,2,..,N),pop为当前读取的出栈序列值
bool is_failed; // 出栈序列成立与否的标志
stack<int> sta;
cin >> M >> N >> K;
for (int i = ; i < K; i++)  // 循环输入K组待判定的出栈序列
{
is_failed = false;
obtain = ;
for (int j = ; j < N; j++) // 循环读取每个出栈序列值
{
cin >> pop;
while (sta.size() <= M && !is_failed) // 栈未满且未确认出栈序列不成立
{
if (sta.empty() || pop != sta.top()) // 栈为空或当读取的出栈序列值与栈顶元素不相等时,把顺序值temp压栈并递增
{
sta.push(obtain++);
}
else // 当前读取的出栈序列值与栈顶元素相等时出栈,跳出循环继续读取下一个出栈序列值
{
sta.pop();
break;
}
}
if (sta.size() > M)
{
is_failed = true;  // 确认出栈序列不成立
}
}
if (is_failed) cout << "NO" << endl;
else cout << "YES" << endl;
while (!sta.empty()) sta.pop(); // 清空栈,因为下一次匹配还要用
}
return ;
}

Pop Sequence的更多相关文章

  1. 1051. Pop Sequence

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

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

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

  4. Pop Sequence (栈)

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

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

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

  7. PAT1051:Pop Sequence

    1051. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

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

  9. 数据结构习题Pop Sequence的理解----小白笔记^_^

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

随机推荐

  1. Oracle 触发器,事物

    触发器:自动执行,可以实现表的约束. 1.行级触发器: CREATE OR REPLACE TRIGGER del_deptid AFTER DELETE ON deptment --触发器条件 DE ...

  2. C# string[,]与string[][]的区别

    对于这两者的区别: 1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作 static void Main(string[] args) { // ...

  3. CSS3 transform的skew属性值图文详解

    我刚刚接触transform的skew属性值时一头雾水,根本不知道种东西到底是咋变的.上网查,各个网站上也只说这个使用来做扭曲变换的,具体是咋变的就是不说....无奈我只好自己研究了,现把研究结果共享 ...

  4. 使用Spring Aop验证方法参数是否合法

    先定义两个注解类ValidateGroup 和 ValidateFiled ValidateGroup .java package com.zf.ann; import java.lang.annot ...

  5. map遍历的四种方法

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  6. C++ Primer : 第九章 : vector变长、string的其他操作以及容器适配器

    vector变长机制.string的其他构造方法,添加.替换和搜索操作,string比较和数值转换,最后是容器适配器. vector对象是如何增长的 vector和string类型提供了一些成员函数, ...

  7. HTTP详解(3)-http1.0 和http1.1 区别

    HTTP详解(3)-http1.0 和http1.1 区别 分类: 网络知识2013-03-17 16:51 1685人阅读 评论(0) 收藏 举报   目录(?)[+]   翻了下HTTP1.1的协 ...

  8. c#部分---需要实例化的内容;

    需要初始化的: 随机数类:初始化 实例化//不允许将初始化的语句放置在循环中//Random ran = new Random(); 时间值类:/初始化 实例化//DateTime dt = new ...

  9. 工作中遇到的问题--缓存配置(使用@Configuration装配 @Bean的方式注入)

    @EnableCaching@Configurationpublic class MFGCachingConfiguration { @Autowired private MFGSettings mf ...

  10. JavaWeb学习记录(十九)——jstl自定义标签库之传统标签

    一.传统标签 (1)JSP引擎将遇到自定义标签时,首先创建标签处理器类的实例对象,然后按照JSP规范定义的通信规则依次调用它的方法. public void setPageContext(PageCo ...