this problem is a very classic problem which can use stack to solve. the problem can be searched through many source site.

#include<cstdio>
#include<stack>//表明要调用关于栈的文件吧;
using namespace std;
const int maxn=+;
使用栈
int n,target[maxn];
int main()
{
while(scanf("%d",&n)==)
{
stack<int> s;//生成一个元素为int的栈吧;
int A=,B=;
for(int i=;i<=n;i++)
scanf("%d",&target[i]);//读入出栈顺序;
int ok =;
while(B<=n)
{
if(A==target[B]){A++;B++;}//进等于出就直接出;
else if(!s.empty()&&s.top()==target[B])
{s.pop();B++;}//如果S不为空栈并且s的栈顶元素正好为需要的B元素就直接把顶端元素放出去;
else if(A<=n)s.push(A++);//还有合法的未进栈元素就让其进栈;
else {ok=;break;}
}
printf("%s\n",ok?"Yes":"No");
}
return ;
}

the railway problem(the example of stack)的更多相关文章

  1. ZOJ Problem Set - 1004-Anagrams by Stack

     唉!先直接上源码吧!什么时候有时间的再来加说明. #include<iostream> #include<vector> #include<stack> #i ...

  2. ZOJ 1004 Anagrams by Stack

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...

  3. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  4. init/main.c

    /* * linux/init/main.c * * Copyright (C) 1991, 1992 Linus Torvalds */ #include <stdarg.h> #inc ...

  5. Lua 架构 The Lua Architecture

    转载自:http://magicpanda.net/2010/10/lua%E6%9E%B6%E6%9E%84%E6%96%87%E6%A1%A3/ Lua架构文档(翻译) 十 102010 前段时间 ...

  6. UVa120 - Stacks of Flapjacks

    Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...

  7. UVa OJ 120

    Background背景 Stacks and Queues are often considered the bread and butter of data structures and find ...

  8. hdu4338 Simple Path

    Everybody knows that totalfrank has absolutely no sense of direction. Getting lost in the university ...

  9. gdb-example-ncurses

    gdb-example-ncurses http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html 1. The Prob ...

随机推荐

  1. Selenium IDE测试创建

    Selenium IDE 测试创建 涉及使用IDE Selenium创建测试,如下面的步骤 记录和测试添加命令 保存测试记录 保存测试程序 执行测试记录 在测试中记录和添加命令 为了演示目的,我们将利 ...

  2. android studio 中获取sha1

    使用keytool 一.配置环境变量 由于要用到keytool工具,而keytool是jdk里的一个工具,首先将jdk/bin所在的目录加到环境变量的PATH中 看我的keytool所在位置 将所在路 ...

  3. 内核源码分析之tasklet(基于3.16-rc4)

    tasklet是在HI_SOFTIRQ和TASKLET_SOFTIRQ两个软中断的基础上实现的(它们是在同一个源文件中实现,由此可见它们的关系密切程度),它的数据结构和软中断比较相似,这篇博文将分析t ...

  4. 从Count看Oracle执行计划的选择

    一. 前言 在调查一个性能问题的时候,一个同事问道,为什么数据库有些时候这么不聪明,明明表上有索引,但是在执行一个简单的count的时候居然全表扫描了!难道不知道走索引更快么? 试图从最简单的coun ...

  5. DelphiXE8怎么使用调试模式

    需求:在开发Android程序时,大家一直是使用ShowMessage.其实XE是支持下断点的. 操作: 1.小米手机用USB线,连到电脑上. 2.小米手机-设置-关于手机-"MIUI版本& ...

  6. SMTP邮件服务器配置

    QQ个人邮箱使用: smtp.qq.com端口为25 密码是个人邮箱密码 QQ企业邮箱使用: smtp.exmail.qq.com端口为25 密码是邮箱密码 163邮箱使用 smtp.163.com端 ...

  7. 转载robots.txt的学习

    转载原地址: http://www.monring.com/seo/aspdotseo-robot.html 在国内,robots.txt文件,对于用户来说他是个可有可无的东西,也不会有人去看.但对于 ...

  8. UVA10361 - Automatic Poetry

    “Oh God”, Lara Croft exclaims, “it’s one of these dumb riddles again!” In Tomb Raider XIV, Lara is, ...

  9. How to organize the Template Files in C++

    Normally you put class definitions in a header file and method definitions in a source file. Code th ...

  10. 让人眼花缭乱的 RSS 版本0.90、0.91、0.92、0.93、0.94、1.0 和 2.0

    1.0的规范 http://web.resource.org/rss/1.0/spec 2.0的规范 http://cyber.law.harvard.edu/rss/rss.html 一个介绍什么是 ...