Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25817    Accepted Submission(s): 9752

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 o
ut
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.".

这个题意说简单形象点就是火车进站,只有一条路,先进站的后出站,输入的前面字符串时进站的顺序,后面是出站的顺序,问是否可按后面顺序出来!

显然这是一个典型的栈的问题!

 #include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
char a[],b[];
int c[];
int main()
{
int n,i,j,k;
stack<char>s;//初始化一个栈
while(scanf("%d",&n)!=EOF)
{
getchar();
scanf("%s%s",a,b);
memset(c,-,sizeof(c));
while(!s.empty())
s.pop();
i=j=k=;
while(i<n)
{
s.push(a[i++]);
c[j++]=;
while(!s.empty()&&s.top()==b[k])//为什么要判断是否为空栈?这个是为了跳出while循环,如果是空栈了。就没栈顶了,没比较的意义了!
{
k++;
c[j++]=;
s.pop();//消栈
}
}
if(!s.empty())
{
printf("No.\n");//如果栈不为空,就是没消完
printf("FINISH\n"); }
else
{
printf("Yes.\n");
for(i=;i<j;i++)
{
if(c[i]==)
printf("in\n");
else if(c[i]==)
printf("out\n"); }
printf("FINISH\n");
}
}
return ;
}

Train Problem I--hdu1022(栈)的更多相关文章

  1. 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 ...

  2. (stack)Train Problem I hdu1022

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

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

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

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

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

  5. hdu1022 Train Problem I---模拟栈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意: 车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若 ...

  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 1022 栈模拟

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

  9. hdu 1022 Train Problem I(栈)

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

随机推荐

  1. SQL SERVER中如何格式化日期

    1. SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)    -- Oct 2 2008 11:01AM  ...

  2. Spring连接MySQL、Oracle和SQL Server的数据库运动连接属性

    在配置文件applicationContext.xml设置如下:<?xml version="1.0" encoding="UTF-8"?>< ...

  3. 几个makefile小例子

    http://www.blogjava.net/canvas/articles/quick_makefile.html http://www.cnblogs.com/azraelly/archive/ ...

  4. Tomcat学习—Tomcat的简介和目录以及配置文件介绍(Windows环境)

    tomcat学习(8) 版权声明:本文为博主原创文章,未经博主允许不得转载. 今天学习TOMCAT,主要学习的是Tomcat的目录结构,配置文件! 1:Tomcat简介 Tomcat 服务器是一个免费 ...

  5. 通过Alexa API获取Alexa排名

    我们通会用Alexa的网站(或其它站长工具网站)来栓查我们的网​站流量排名,这样就必须去那些网站.实际上,可以通过Alexa XML API 获取网站的Alexa相关的数据(XML格式的),再使用XM ...

  6. ubuntu 安装openproj-1.4-2.noarch.rpm

    一 openproj是rpm包,ubuntu下需要转成deb安装.具体步骤1:下载:http://sourceforge.net/projects/openproj/2:安装alien sudo ap ...

  7. iframe父子页面互调方法和属性

    1.iframe子页面调用 父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: window.parent.a(); 子页面取父页面中的标签 ...

  8. 瑞柏匡丞:App对新媒体的影响

    当下App的迅猛发展是媒体进入开放平台时代的折射,作为最具新媒体特质的代表,App充满了社交性与交互性,并有效整合了传统媒体和新媒体的内容和服务.“什么是新媒体”——这个新媒体时代最为核心的命题,实际 ...

  9. 锁sql server锁

    锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...

  10. java_IO流之 NIO

    NIO 定义 即新IO,在JDK1.4的java.nio.*包中引入,其目的在于提高速度. 在Java1.4之前的I/O系统中,提供的都是面向流的I/O系统,系统一次一个字节地处理数据,一个输入流产生 ...