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. fread与fwrite的自我理解

    size_t  fread(void* buff,size_t size,size_t count,FILE* stream) 参数1:读取到该buff所指向的内存空间中 参数2:每次读取的字节数,单 ...

  2. css3选择器(下)

    1.:enabled选择器 在Web的表单中,有些表单元素有可用(“:enabled”)和不可用(“:disabled”)状态,比如输入框,密码框,复选框等.在默认情况之下,这些表单元素都处在可用状态 ...

  3. springday05-go1

    新建web工程spring-netcross1.导入spring文件夹里的七个jar包,另外还要导入jdbc-lib的四个jar包,ojdbc.jar,commoms-pool,commons-dbc ...

  4. SparkSQL基础应用(1.3.1)

    一.概述 从1.3版本开始Spark SQL不再是测试版本,之前使用的SchemaRDD重命名为DataFrame,统一了Java和ScalaAPI. SparkSQL是Spark框架中处理结构化数据 ...

  5. -XX:+printGC

    -XX:+printGC 可以打印GC的简要信息[GC 4790K->374K(15872K), 0.0001606 secs][GC 4790K->374K(15872K), 0.000 ...

  6. ThinkPHP讲解(七)——修改删除

    修改数据 方式一:数组方式,直接将数据库里需要修改的内容进行修改 function Update() { //1.数组方式 $model=D("Info"); $attr=arra ...

  7. PDO讲解

    PDO的知识点标注在代码里 <?php //造DSN:驱动名:dbname=数据库名:host=服务器地址 $dsn="mysql:dbname=mydb;host=localhost ...

  8. ionic之AngularJS扩展 移动开发(视图导航一)

    目录: 内联模板 : script 路由机制 : 状态机 导航视图 : ion-nav-view 模板视图 : ion-view 导航栏 : ion-nav-bar 回退按钮 : ion-nav-ba ...

  9. 对Alexia(minmin)网友代码的评论及对“求比指定数大且最小的‘不重复数’问题”代码的改进

    应Alexia(minmin)网友之邀,到她的博客上看了一下她的关于“求比指定数大且最小的‘不重复数’问题”的代码(百度2014研发类校园招聘笔试题解答),并在评论中粗略地发表了点意见. 由于感觉有些 ...

  10. 不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数

    不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数. 这是一道面试题,可以使用递归的方式解答,答案如下: #include <stdio.h> int mylen(cha ...