题目链接: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. mysql 中如何查找相同的数据

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbcAAAEYCAIAAABQvy+HAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu

  2. codevs1322 单词矩阵

    题目描述 Description 对于包含字母A到Y各一次的单词S,将其从上到下从左到右写在一个5*5的矩阵中,如单词ADJPTBEKQUCGLRVFINSWHMOXY写出来如下: A D J P T ...

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

  4. hihoCoder 1195 高斯消元.一

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:喂不得了啦,那边便利店的薯片半价了! 小Hi:啥?! 小Ho:那边的便利店在打折促销啊. 小Hi:走走走, ...

  5. IOS基础之(十四) KVO/KVC

    资料参考: http://www.cnblogs.com/kenshincui/p/3871178.html http://www.cnblogs.com/stoic/archive/2012/07/ ...

  6. POJ2392Space Elevator(贪心+背包)

    Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9970   Accepted: 4738 De ...

  7. tomcat架构

    很多开源应用服务器都是集成tomcat作为web container的,而且对于tomcat的servlet container这部分代码很少改动.这样,这些应用服务器的性能基本上就取决于Tomcat ...

  8. SSH协议及其应用

    SSH协议及其应用 原文作者:阮一峰 链接: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruany ...

  9. Netbeans连接数据库

    /* Netbeans连接数据库 NetBeans项目的“项目属性”中“库”一栏中.Tab页“编译和运行”中已经加上jdbc的驱动文件 */ Connection conn = null;//连接数据 ...

  10. VirtualBox下安装rhel5.5 linux系统

    以前也用过VMware server和VMware workstation虚拟机,现在使用了一段时间VirtualBox,感觉它比较轻巧,很适合我,在Win7系统下用起来很方便.下面详细介绍下在Vir ...