栈之火车进站和出站

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

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

题目大概意思:有N辆火车,以序列1方式进站,判断是否能以序列2方式出栈。进站不一定是一次性进入,也就是说中途可以出站。

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
int main(void)
{
stack<char>s1;
stack<char>s2;
int n,i,j,t,a[1000];
char c[100010],d[100010];
while(scanf("%d",&n)!=EOF)
{j=0;
scanf("%s",c);
scanf("%s",d);
for(i=n-1;i>=0;i--)
s1.push(d[i]);
for(i=0;i<n;i++)
{
s2.push(c[i]);
a[j++]=1;
while(!s2.empty()&&s1.top()==s2.top())
{
s1.pop();
s2.pop();
a[j++]=0;
}
}
if(s1.empty()&&s2.empty())
{
printf("Yes.\n");
for(i=0;i<2*n;i++)
{
if(a[i]==1) printf("in\n");
else printf("out\n");
}
}
else printf("No.\n");
printf("FINISH\n");
}
return 0;
}                答案错误

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
int main(void)
{
stack<char>s1;
stack<char>s2;
int n,i,j,t,a[1000];
char c[100010],d[100010];
while(scanf("%d",&n)!=EOF)
{j=0;
scanf("%s",c);
scanf("%s",d);
for(i=n-1;i>=0;i--)
s1.push(d[i]);
for(i=0;i<n;i++)
{
s2.push(c[i]);
a[j++]=1;
while(!s2.empty()&&s1.top()==s2.top())
{
s1.pop();
s2.pop();
a[j++]=0;
}
}
if(s1.empty()&&s2.empty())
{
printf("Yes.\n");
for(i=0;i<2*n;i++)
{
if(a[i]==1) printf("in\n");
else printf("out\n");
}
}
else

{

printf("No.\n");

while(!s1.empty())
s1.pop();                      如果不能实现 ,那也就是说两个栈没有清空 ,必须清空才能继续下一个实例;             

while(!s2.empty())
s2.pop();

}

printf("FINISH\n");

}
return 0;
}

时间: 2024-10-13 02:11:28

栈之火车进站和出站的相关文章

栈:火车进站

题目描述 这里有n列火车将要进站再出站-- 但是,每列火车只有1节---那就是车头-- 描述 有n列火车按1到n的顺序从东方左转进站,这个车站是南北方向的,它虽然无限长,只可惜是一个死胡同,而且站台只有一条股道,火车只能倒着从西方出去,而且每列火车必须进站,先进后出. (某生:不就是个栈吗?每次可以让右侧头火车进栈,或者让栈顶火车出站? 老师:闭嘴!) 就像这样: 出站<---    <--进站 |车| |站| |__| 现在请你按<字典序>输出前20种可能的出栈方案. 输入 一个

CodeVS3958 火车进站

3958 火车进站 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 火车站内往往设有一些主干线分叉出去的铁路支路,供火车停靠,以便上下客或装载货物.铁路支路有一定长度:火车也有一定的长度,且每列火车的长度相等. 假 设某东西向的铁路上,有一小站.该站只有一条铁路支路可供火车停靠,并且该铁路支路最多能容纳M 辆火车.为了火车行驶的通畅,该站只允许火车自东方进站,自西方出站,且先进站的火车必须先出站,否则,站内火车将发生堵塞.该

(hdu)1022 Train Problem I 火车进站问题

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 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 Stati

C++ HOJ 火车进站

[问题描写叙述] 给定一个正整数N代表火车数量.0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号. 要求以字典序排序输出火车出站的序列号. 输入: 有多组測试用例,每一组第一行输入一个正整数N(0<N<10),第二行包含N个正整数,范围为1到9. 输出: 输出以字典序排序的火车出站序列号,每一个编号以空格隔开,每一个输出序列换行.详细见sample. 例子输入: 3 1 2 3 例子输出: 1 2 3 1 3 2 2 1 3 2 3 1 3 2 1 [

火车进站

描述 给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号. 知识点 栈 运行时间限制 0M 内存限制 0 输入 有多组测试用例,每一组第一行输入一个正整数N(0<N<10),第二行包括N个正整数,范围为1到9. 输出 输出以字典序排序的火车出站序列号,每个编号以空格隔开,每个输出序列换行,具体见sample. 样例输入 3 1 2 3 样例输出 1 2 3 1 3 2 2 1 3 2 3

华为OJ—火车进站(栈,字典排序)

给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号.其实也就是输出所有可能的出栈序列. 样例输入: 3 1 2 3 样例输出: 1 2 31 3 22 1 32 3 13 2 1 解答: 其实核心就是一个栈,对于第K个数,在第K个数进栈之前,前面的 K-1 个数要么全部出去了,要么都在栈里面,要么部分在栈里面部分出去了.那么可以假想,在第K个数入栈之前,依次从栈里面出去 0个.1个.2个--栈.si

火车出站

火车出站 题目描述 铁路进行列车调度时,常把站台设计成栈式结构的站台,试问:设有编号为1到n的n辆列车,顺序开入栈式结构的站台,则可能的出栈序列有多少种? 输入 输入包含多组测试数据.每组为一个正整数n(1<=n<=20),表示有n辆列车. 输出 输出可能的出栈序列有多少种. 样例输入 4 3 样例输出 14 5 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int n; 6 while(cin>

[华为机试练习题]13.火车进站

题目 描述: 给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号. 题目类别: 栈 难度: 高级 运行时间限制: 10Sec 内存限制: 128MByte 阶段: 入职前练习 输入: 有多组测试用例,每一组第一行输入一个正整数N(0<N<10),第二行包括N个正整数,范围为1到9. 输出: 输出以字典序排序的火车出站序列号,每个编号以空格隔开,每个输出序列换行,具体见sample. 样例

火车进站问题

import java.util.EmptyStackException;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;import java.util.Stack;public class H06 { /** * @return * @给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列, * 一共N辆火车,每辆火车以数字1-9编号. * 要求以字典序排序输出火车出站的序