Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25029    Accepted Submission(s): 9445

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
/*
模拟题,利用栈,最先出栈的在最前,不断的进栈,同时比较栈头与出栈的元素是不是相同的,
如果相同则出栈,最后如果还有剩余元素,则不符合
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack> using namespace std; const int Max=1100000; char IN[15];
char OUT[15];
string str[110];
int main()
{
int n;
while(~scanf("%d %s %s",&n,IN,OUT))
{
int top=0;
stack<char>a;
int i=0,j=0;
while(i<n&&j<n)
{
if(a.empty()||a.top()!=OUT[j])
{
a.push(IN[i++]);
str[top++]="in";
}
else
{
a.pop();
j++;
str[top++]="out";
}
}
while(j<n&&!a.empty()&&a.top()==OUT[j])
{
a.pop();
j++;
str[top++]="out";
}
if(a.empty())//判断是否符合
{
cout<<"Yes."<<endl;
for(int i=0;i<top;i++)
{
cout<<str[i]<<endl;
}
cout<<"FINISH"<<endl;
}
else
{
cout<<"No."<<endl<<"FINISH"<<endl;
}
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Train Problem I 分类: HDU 2015-06-26 11:27 10人阅读 评论(0) 收藏的更多相关文章

  1. Retinex系列之McCann99 Retinex 分类: 图像处理 Matlab 2014-12-03 11:27 585人阅读 评论(0) 收藏

    一.McCann99 Retinex McCann99利用金字塔模型建立对图像的多分辨率描述,自顶向下逐层迭代,提高增强效率.对输入图像的长宽有 严格的限制,要求可表示成 ,且 ,. 上述限制来源于金 ...

  2. short-path problem (Spfa) 分类: ACM TYPE 2014-09-02 00:30 103人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include <queue> #i ...

  3. short-path problem (Floyd) 分类: ACM TYPE 2014-09-01 23:58 100人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  4. short-path problem (Dijkstra) 分类: ACM TYPE 2014-09-01 23:51 111人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  5. 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 346人阅读 评论(0) 收藏

    在百度编辑器示例代码基础上进行了修改,封装成类库,只需简单配置即可使用. 完整demo下载 版权声明:本文为博主原创文章,未经博主允许不得转载.

  6. Crashing Robots 分类: POJ 2015-06-29 11:44 10人阅读 评论(0) 收藏

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8340   Accepted: 3607 D ...

  7. Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  8. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  9. Segment Tree with Lazy 分类: ACM TYPE 2014-08-29 11:28 134人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...

随机推荐

  1. [reprint]useful linux commands

    linux一说都是搞开发玩的,敲敲键盘就能完成所有的工作.其实你也可以这么玩,玩游戏的除外哦. 那我们就来侃侃如何玩,linux是命令的天下,高级的命令那是相当的多,但是我们正真用到的也就那么几个看你 ...

  2. zjuoj 3609 Modular Inverse

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...

  3. [转] MongoDB shell 操作 (查询)

    最近有用到mongoDB,每次都去查看官方文档很是费劲,自己准备写点东西.但在博客园上看到另外的一篇博文不错,就转载过来,加上点儿自己的修饰 左边是mongodb查询语句,右边是sql语句.对照着用, ...

  4. Java基础(7):二维数组初始化时需要注意的问题

    二维数组可以先指定行,再指定列:但不能先指定列,再指定行 没有说明二维数组的行的个数,在定义二维数组时也可以只指定行的个数,然后再为每一行分别指定列的个数.如果每行的列数不同,则创建的是不规则的二维数 ...

  5. android测试(转)

    1.冒烟测试 跟web端的测试流程一样,你拿到一个你们开发做出来的apk首先得去冒烟,也就是保证他的稳定性,指定时间内不会崩溃.这款原生sdk自带的monkey可以当做我们的测试工具.就跟我之前博客所 ...

  6. 是否可以继承String类?

    是否可以继承String类? String类是final类故不可以继承

  7. java.注释类型

    单行注释  //注释内容 多行注释 /*注释内容*/ 文档注释 /**注释内容*/

  8. 从一个例子讲解拷贝构造函数与return

    #include "iostream" using namespace std; class Location { public: Location(, ) { X = xx; Y ...

  9. Oracle中游标返回多条数据的情况

    DECLARE -- 定义类型. TYPE test_type IS TABLE OF test_main%ROWTYPE; test_data test_type; -- 定义游标. CURSOR ...

  10. clock gating and PLL

    一个gating的clock是指:clock network除了包含inverter和buffer外,还有其他logic. PrimeTime会自动的对gating input进行setup和hold ...