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. 试验Windows Embedded Standard 7 Service Pack 1 Evaluation Edition

    =========================================== 是否支持再使用 RT 7 Lite 精简 ? ================================= ...

  2. JavaScript UI技术选型

    ExtJS l ExtJS(TODO:找旧版本,类似现在EasyUI插件的旧版本)简介:纯JS支持:IE6授权:GPLv3授权.商业授权($329/人) l Ext.NET简介:ExtJS的NET封装 ...

  3. SVM:从理论到OpenCV实践

    (转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途)   一.理论 参考网友的博客: (1)[理论]支持向量机1: Maximum Marg ...

  4. OpenGL学习——基本概念和坐标变换

    基本概念 基本功能:几何图形.变换.着色.光照.贴图 高级功能:曲面图元.光栅操作.景深.shader编程   状态机 先设置状态参数:多边形.顶点列表.填充颜色.纹理.混合模式.坐标系 再调用绘图指 ...

  5. Chapter4:表达式

    左值和右值 当一个对象被用作右值的时候,用的是对象的值(内容),当对象被用作左值的时候,用的是对象的身份(在内存中的位置). 一个重要的原则是需要右值的地方可以用左值来代替,但是不能把右值当作左值使用 ...

  6. Hadoop 2.2 YARN分布式集群搭建配置流程

    搭建环境准备:JDK1.6,SSH免密码通信 系统:CentOS 6.3 集群配置:NameNode和ResourceManager在一台服务器上,三个数据节点 搭建用户:YARN Hadoop2.2 ...

  7. APACHE如何里一个站点绑定多个域名?用ServerAlias

    APACHE2如何里一个站点绑定多个域名?用ServerAlias以前很笨,要使多个域名指向同一站点总是这样写: <VirtualHost *:80>ServerAdmin i@kuigg ...

  8. Nginx和Tengine的详细安装图文教程(Linux下)

    简洁安装 安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel 编译三步走./configure \ --prefix=/opt/sx ...

  9. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  10. javascript中sleep等待实现

    function sleep(milliSeconds) { var startTime = new Date().getTime(); while (new Date().getTime() < ...