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

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.". 题目意思:给了火车进站和出站的顺序,问是否匹配,同时也要给出进出站的步骤。 解题思路:火车进出站实际上也模拟了栈的使用,我们可以对比两个字符串,s1串为进站顺序,s2串为出站顺序,对s1中每一个字符(火车)
比较s2的出站顺序,不相同则是进站,相同就要出站,以此类推,用一个数组标记进站和出站即可。 上代码:
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
int n,i,j,k,flag[];
char s1[],s2[];
while(scanf("%d %s%s",&n,s1,s2)!=EOF)
{
stack<char>s;
memset(flag,-,sizeof(flag));
j=;
k=;
for(i=;i<n;i++)
{
s.push(s1[i]);
flag[k++]=;
while(!s.empty()&&s.top()==s2[j])
{
s.pop();
flag[k++]=;
j++;
}
}
if(j==n)
{
printf("Yes.\n");
for(i=;i<k;i++)
{
if(flag[i]==)
printf("in\n");///1为进栈
else
printf("out\n");///0为出栈 }
}
else
printf("No.\n");
printf("FINISH\n");
}
return ;
}


Train Problem(栈的应用)的更多相关文章

  1. Train Problem I(栈)

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

  2. Train Problem I--hdu1022(栈)

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

  3. train problem I (栈水题)

    杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...

  4. Hdu 1022 Train Problem I 栈

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

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

  6. HDU - 1022 Train Problem I STL 压栈

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

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

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

  8. hdu 1022:Train Problem I(数据结构,栈,递归,dfs)

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

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

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

  10. HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]

    Train Problem I 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...

随机推荐

  1. Visual Studio中添加API断点

    如:添加 PostMessageA 断点 {,,USER32.DLL}_PostMessageA@16 //判断为WM_CLOSE消息*(int*)(esp + 8) == 0x0010

  2. WebService 学习笔记(一、概念及定义)

    定义 WebService是一种服务导向架构(SOA service-oriented architecture)的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作. Web ...

  3. 怎么将oracle的sql文件转换成mysql的sql文件

    怎么将sql文件导入PowerDesigner中的方法(将oracle的sql文件转换成mysql的sql文件)呢? 怎么将xx.sql文件的数据库结构导入powerdesigner 的方法呢? 现讲 ...

  4. 虚拟机下linux 系统网卡配置、固定IP地址

    1.进入该目录下修改内容 vi       /etc/sysconfig/network-scripts/  ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEF ...

  5. Macbook下安装memcached

    参考文献: https://blog.csdn.net/weixin_41827162/article/details/82049520 感谢大佬 安装memcached需要Homebrew 注意点: ...

  6. 让pandas的输出结果中显示全部数据

    import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) ...

  7. FIFO队列 ADT接口 链表实现

    FIFO.h (接口) #include "Item.h" void QUEUinit(int); int QUEUempty(void); void QUEUput(Item); ...

  8. C指针(4)——数据结构中指针的应用(非常重要)

    5-1动态内存分配,分配的是堆内存的空间 分配内存函数 (都集中在库函数 stdlib.h  中) void *malloc (unsigned int num_bytes); //指定分配内存空间大 ...

  9. (数据科学学习手札46)Scala中的面向对象

    一.简介 在Scala看来,一切皆是对象,对象是Scala的核心,Scala面向对象涉及到class.object.构造器等,本文就将对class中的重点内容进行介绍: 二.Scala中的类 2.1 ...

  10. P1803 凌乱的yyy

    P1803 凌乱的yyy 题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) ...