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. Tomcat类加载器机制

    Tomcat为什么需要定制自己的ClassLoader: 1.定制特定的规则:隔离webapp,安全考虑,reload热插拔 2.缓存类 3.事先加载 要说Tomcat的Classloader机制,我 ...

  2. Java FX中TreeView节点选中和双击事件监听

    TreeItem<String> treeRoot = new TreeItem<String>("Root"); treeRoot.setExpanded ...

  3. swift语言实战晋级-第9章 游戏实战-跑酷熊猫-9-10 移除平台与视差滚动

    9.9 移除场景之外的平台 用为平台是源源不断的产生的,如果不注意销毁,平台就将越积越多,虽然在游戏场景中看不到.几十个还看不出问题,那几万个呢?几百万个呢? 所以我们来看看怎么移除平台,那什么样的平 ...

  4. Winform 基本属性

    WinForm--- 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执行 WinFo ...

  5. 二分多重匹配(HDU5093)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  6. Java基础(63):正则表达式的运用

    Java 正则表达式 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. Java正则表达式和Perl的是最为相似 ...

  7. .NET: WPF Template

    Data Template: 要做一个listBox,里面有车子的简单信息,点了里面的item后就会显示详细信息. car class: using System; using System.Coll ...

  8. poj: 2739

    挺简单,生成素数表之后建个全素数的vector,然后..随便玩咯 #include <iostream> #include <stdio.h> #include <str ...

  9. Android activity的回传数据

    package com.example.myact3; import android.content.Intent; import android.os.Bundle; import android. ...

  10. sql 中的回车和换行问题

    --移除回车符 update master_locationSET street_number = REPLACE(street_number, CHAR(13), '') --移除换行符 updat ...