Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one
railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train
B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the
trains can get out in an order O2.

 
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample
Input.
 
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of
the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 
Sample Input
3 123 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
Hint
Hint
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".
 

栈的简单应用,本题具体解答在白书上有.

以下附上代码:用c++交会错。用G++就不会。。

。求解释

#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
int a[10000],ans[20000],b[10000];
stack<int>ss;
int main()
{
int n,i,j,k;
string s1,s2;
while(scanf("%d",&n)!=EOF){
cin>>s1>>s2;
for(i=1;i<=n;i++) a[i]=s1[i-1]-'0';
for(j=1;j<=n;j++) b[j]=s2[j-1]-'0';
while(!ss.empty()) ss.pop();
k=0,j=1;
for(i=1;i<=n;i++) {
ss.push(a[i]);
ans[++k]=1;
while(!ss.empty() && ss.top()==b[j]) {
ss.pop();
ans[++k]=2;
j++;
}
}
if(k==2*n) {
printf("Yes.\n");
for(i=1;i<=2*n;i++) {
if(ans[i]==1) printf("in\n");
else printf("out\n");
}
}
else printf("No.\n");
printf("FINISH\n");
}
return 0;
}

HDU Train Problem I (STL_栈)的更多相关文章

  1. HDU Train Problem I 1022 栈模拟

    题目大意: 给你一个n 代表有n列 火车,  第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序, 分析一下就知道这,这个问题就是栈, 先进后出吗, ...

  2. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  3. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  4. hdu 1022 Train Problem I(栈的应用+STL)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Train Problem I--hdu1022(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  7. HDOJ/HDU 1022 Train Problem I(模拟栈)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  8. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  9. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

随机推荐

  1. C++表达式求值(利用数据结构栈)

    唉,刚刚用C++又又一次写了一个较完好的表达式求值程序,最后精简后程序还不到100行.这不经让我 想到了大一上学期刚学c语言时自己费了好大的劲,写了几百行并且功能还不是非常齐全(当时还不能计算有括号的 ...

  2. iOS-为方便项目开发在pch加入一些经常使用宏定义

    1.关于NSLog输出 /** * 当Xcode为Release时不输出,为Debug时输出 * * @param ... * * @return */ #ifndef __OPTIMIZE__ #d ...

  3. cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第一步---開始界面&amp;关卡选择

    /* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦.他说:随便写,第一别全然照搬代码:第二能够说 ...

  4. java基础——各种变量你晕了不?

    java 中的变量大致分为 成员变量 和 局部变量 两大类. 成员变量:     在类体里面定义的变量称为成员变量.     假设该成员变量有 static keyword修饰.则该成员变量称为 静态 ...

  5. 0x27 A*

    终于完全了解A*到底是什么玩意儿了 对于当前的决策,选取当前花费+预估花费最小来拓展. 因为假如预估出现失误,那么很可能就会延伸到一个错误的决策点,而这个决策点偏偏就是ed,而由于预估失误,其他点的当 ...

  6. Spark编程模型几大要素

    不多说,直接上干货! Spark编程模型几大要素 Driver Program 输入-Transformation-Action 缓存 共享变量

  7. 学习篇之SVG

    学习篇之SVG 一.use重用 与 g组合 xmlns变量实际上指示浏览器如何解释称为SVG的XML方言 <g></g> 组合 <use /> 重用 <ell ...

  8. runloop源代码

    https://github.com/zzf073/runloopDemo /** *  调度例程 *  当将输入源安装到run loop后,调用这个协调调度例程,将源注册到客户端(可以理解为其他线程 ...

  9. 使用 Sublime 或其他编辑器调试 Tampermonkey 油猴脚本

    作者说由于 Chrome 安全限制,没办法调用外部编辑器调试,但提供了一个间接办法,那就是脚本中使用@require file:///引入本地文件的形式,具体的方法是 打开 chrome://exte ...

  10. 迁移IPv6:6To4隧道技术

    1. IPv6 路由选择协议 首先要讨论的是RIPng(下一代).RIP非常适合用于小型网络.这正是它没有惨遭淘汰,继续用于 IPV6网络的原因.另外,还有EIGRPv6,因为它有独立于协议的模块,只 ...