题目链接:http://poj.org/problem?id=1363

题意:有一列火车,车厢编号为1~n,从A方向进站,向B方向出站。现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理。

实际上是模拟栈的过程,而栈的特点是先进后出。另外一个麻烦的地方就是输入输出格式问题。

本题实现提供两种方法:没有用到STL栈和有用到STL栈

 #include <iostream>      // 法二:头文件多包含一个 #include <stack>
using namespace std; const int maxn = + ; int main()
{
int A, B, i, n, top, target[maxn], stack[maxn]; // 法二:数组stack去掉, 变成 stack<int> s
while (scanf("%d", &n) && n)
{
while ()
{
int flag = ;
for (i = ; i <= n; i++)
{
scanf("%d", &target[i]);
if (target[] == )
{
flag = ;
break;
}
}
if (!flag)
{
printf("\n");
break;
}
int ok = ;
A = B = ;
top = ;
while (B <= n)
{
if (A == target[B]) // 处理1~n的特殊情况
{
A++, B++;
}
else if (top && stack[top] == target[B]) // top && !s.empty() && s.top() == target[B] 出栈
{
top--; // s.pop();
B++;
}
else if (A <= n)
{
stack[++top] = A; // s.push(A); 进栈
A++;
}
else
{
ok = ;
break;
}
}
if (ok)
printf("Yes\n");
else
printf("No\n");
}
}
return ;
}

poj 1363 Rails 解题报告的更多相关文章

  1. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  2. POJ 1363 Rails(栈)

    题目代号:POJ 1363 题目链接:http://poj.org/problem?id=1363 题目原题: Rails Time Limit: 1000MS   Memory Limit: 100 ...

  3. OpenJudg / Poj 1363 Rails

    1.链接: http://poj.org/problem?id=1363 http://bailian.openjudge.cn/practice/1363 2.题目: Rails Time Limi ...

  4. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  5. poj 2051.Argus 解题报告

    题目链接:http://poj.org/problem?id=2051 题目意思:题目有点难理解,所以结合这幅图来说吧---- 有一个叫Argus的系统,该系统支持一个 Register 命令,输入就 ...

  6. poj 1102.LC-Display 解题报告

    题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...

  7. POJ 1363 Rails

    Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21728   Accepted: 8703 Descriptio ...

  8. POJ 1840 Eps 解题报告(哈希)

    a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,xi∈[-50,50],且xi!=0.让我们求所有解的可能. 首先,如果暴力判断的话,每个x的取值有100种可能,100^5肯定 ...

  9. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

随机推荐

  1. STL简单应用问题

    问题: Input输入的第一行是一个整数T( 1 <= T <= 100 ),表示有几组输入数据.每组输入由4部分组成:(1)一个字典,最多包含2000个单词,每个单词一行.(2)一行字符 ...

  2. Spring的辅助类

    http://www.cnblogs.com/maoan/p/3446224.html spring获取ApplicationContext对象的方法——ApplicationContextAware

  3. C++ STL初学笔记

    C++  STL初学笔记 更系统的版本见徐本柱的PPT set 在这儿:http://www.cnblogs.com/pdev/p/4035020.html #include <vector&g ...

  4. VMWare File Format Learning && Use VHD File To Boot VMWare && CoreOS Docker Configuration And Running

    目录 . Virtual Machine Introduce . Vmware Image File Format . VHD File Format . Convert VHD File Into ...

  5. UVA1220Party at Hali-Bula(树的最大独立集 + 唯一性判断)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/H 紫书P282 员工和直属老板只能选一个,最多选多少人 思路 ...

  6. SQL localdb 连接字符串

    http://blog.csdn.net/greystar/article/details/47699797 原来SQL 2012 下连接LOCALDB,字符串为: Data Source=(Loca ...

  7. 工匠若水 Android应用开发编译框架流程与IDE及Gradle概要

    http://blog.csdn.net/yanbober/article/details/45306483 http://blog.csdn.net/yanbober/article/details ...

  8. 裴波那契数列 JavaScript 尾递归实现

    一般递归实现 : //经典递归 function fibonacci(n) { return (function(n) { ) ; ); })(n); } 或者: function fibonacci ...

  9. Grunt之项目脚手架

    在网上搜了下,grunt这方面的教程挺少的,来去都是一些被频繁转载的文章.唉,人艰不拆啊. 首先我们在全局环境中安装grunt-init. npm install -g grunt-init 来看看官 ...

  10. C#集合实现接口一览表