Uva - 514 - Rails
C是一个栈,每次先检查A的第一个元素是否满足,如果满足,直接进入B;再检查C中栈顶元素是否满足,如果满足,出栈进入B;前两步都不满足将A放入C栈中。循环到B满或者A,C中都不满足条件并且A空,第一种情况成功,第二种情况失败。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
using namespace std;
const int maxn = 1010;
int bRail[maxn];
int main()
{
int n;
while (scanf("%d", &n) == 1 && n) {
stack<int> s;
memset(bRail, 0, sizeof(bRail));
while (scanf("%d", &bRail[1]) == 1 && bRail[1]) {
for (int i = 2; i <= n; i++) {
scanf("%d", &bRail[i]);
}
int A = 1, B = 1;
int ok = 1;
while (B <= n) {
if (A == bRail[B]) { // 先看A中第一个是否满足B
A++;
B++;
}
else if (!s.empty() && s.top() == bRail[B]) { // 再看栈顶车厢是否满足
s.pop();
B++;
}
else if (A <= n) { // 都不满足就让A的车厢入栈
s.push(A++);
}
else { // B没有满,但是没有满足的情况了,失败
ok = 0;
break;
}
}
printf("%s\n", ok ? "Yes" : "No");
}
printf("\n");
}
return 0;
}
Uva - 514 - Rails的更多相关文章
- UVA 514 - Rails ( 铁轨)
from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939523 例题6-2 铁轨(Rails, ACM/ICPC CERC ...
- UVa 514 Rails(经典栈)
Rails There is a famous railway station in PopPush City. Country there is incredibly hilly. The st ...
- UVa 514 Rails(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-514 /* 问题 输入猜测出栈顺序,如果可能输出Yes,否则输出No 解题思路 貌似没有直接可以判定的方法,紫书上给出 ...
- UVA ~ 514 ~ Rails (栈)
参考:https://blog.csdn.net/ZscDst/article/details/80266639 #include <iostream> #include <cstd ...
- UVA - 514 Rails(栈模拟)
题目: 给出一个序列,问将1,2,3,4……按从小到大的顺序入栈,能否得到给出的序列. 思路: 用stack模拟就可以了. 当前的cnt如果小于a[i],就将cnt入栈,否则就判断栈顶是不是和a[i] ...
- 铁轨(rails, ACM/ICPC CERC 1997,Uva 514)
铁轨(rails, ACM/ICPC CERC 1997,Uva 514) 题目描述 某城市有一个火车站,铁轨铺设如图所示.有n节车厢从A方向驶入车站,按进站顺序编号为1~n.你的任务是让它们按照某种 ...
- Rails,uva 514
题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...
- UVa 514 (stack的使用) Rails
练习一下stack的使用,还有要注意一下输入的格式,看了好长时间没懂. //#define LOCAL #include <iostream> #include <cstdio> ...
- 【紫书】Rails UVA - 514 栈
题意:判断出栈顺序是否合法 题解:两个指针,A指向入栈序列,B指向出栈. 的分三种情况:if 1.A==B :直接入栈加出栈即可A++,B++ else 2.和栈顶相同,直接出栈A==stac ...
随机推荐
- java HTTP请求工具
package HttpRequestTest; import java.io.BufferedReader; import java.io.InputStream; import java.io.I ...
- Go 实现 NumberFormat 函数
[转] http://www.syyong.com/Go/Go-implements-the-NumberFormat-algorithm.html NumberFormat - 以千位分隔符方式格式 ...
- javaweb面试题
1.Tomcat的优化经验 答:去掉对web.xml的监视,把JSP提前编辑成Servlet:有富余物理内存的情况下,加大Tomcat使用的JVM内存. 2.什么是Servlet? 答:可以从两个方面 ...
- HTML5 唤起 APP
<p><a href="xxx://app/question/95">点击跳转,直接回帖报名</a></p> /* global n ...
- Linux完全卸载Oracle的操作步骤
卸载步骤如下:1.运行$ORACLE_HOME/bin/localconfig delete2.rm -rf $ORACLE_BASE/*3.rm -f /etc/oraInst.loc etc/or ...
- iOS不能交互的几种情况
alpha <=0.01 hidden = YES userInteraction = NO 父试图不允许交互,子试图也不允许交互: 在父试图可见范围内,可以交互,超出部分失效,不能交互
- JavaC命令不能被执行尴尬问题解决
安装和配置环境变量都按着流程在,但在最后的检验时,发现Java Java -version 都能运行,唯独Javac 报"不能识别命令"错误信息,下面列出我遇到一个尴尬问题 在 ...
- Node.js 工具模块
在 Node.js 模块库中有很多好用的模块.接下来我们为大家介绍几种常用模块的使用: 序号 模块名 & 描述 1 OS 模块 提供基本的系统操作函数. 2 Path 模块提供了处理和转换文件 ...
- Android Studio: You need to use a Theme.AppCompat theme (or descendant) with this activity.
错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...
- Rails报找不到sanitize和raw方法的解决
以下一段代码作用是对html字符串做过滤作用: sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' '))) 不过实际会报 ...