Train Problem I

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

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

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

Author

Ignatius.L

就是要判断这样的入栈顺序能否可以的出那样的出栈顺序。

AC代码如下:

//1022
#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        char in[10],out[10],s[10];   //in:入栈顺序  out:出栈顺序  s:栈
        int p[1000],top=0,i=0,j=0,k=0;  //p:记录出入栈情况
        scanf("%s %s",in,out);
        //printf("%d\n%s\n%s\n",n,in,out);
        while(i<=n)
        {
            if(k>0&&s[k-1]==out[j])
            {
                k--;
                j++;
                p[top]=0;
                top++;
            }
            else
            {
                s[k]=in[i];
                p[top]=1;
                top++;
                k++;
                i++;
            }

}
        if(j>=n)
        {
            printf("Yes.\n");
            for(i=0;i<top-1;i++)
            {
                if(p[i]==1)
                    printf("in\n");
                else
                    printf("out\n");
            }
            printf("FINISH\n");
        }
        else
            printf("No.\nFINISH\n");

}
    return 0;
}
---------------------
作者:yongtaozheng
来源:CSDN
原文:https://blog.csdn.net/Twinkle_sone/article/details/95165762
版权声明:本文为博主原创文章,转载请附上博文链接!

hdu 1022 Train Problem I【模拟出入栈】的更多相关文章

  1. HDU 1022 Train Problem I 模拟栈题解

    火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...

  2. HDU - 1022 Train Problem I STL 压栈

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

  3. HDOJ/HDU 1022 Train Problem I(模拟栈)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  4. HDU 1022 Train Problem I (数据结构 —— 栈)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

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

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

  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 栈

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

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

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

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

随机推荐

  1. 10-ESP8266 SDK开发基础入门篇--上位机通过串口控制ESP8266灯亮灭

    https://www.cnblogs.com/yangfengwu/p/11087618.html 其实这一节就是对上三节的综合测试 https://www.cnblogs.com/yangfeng ...

  2. 51 NOD 1239 欧拉函数之和(杜教筛)

    1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...

  3. 二八法则(The 80/20 Principle)

    二八法则的定义:在任何一组事物中,最重要的只占其中一小部分,约20%,其余80%尽管占多数,却是次要的. 二八法则的例子:社会上20%的人占有80%的社会财富 20%的工厂有80%的产出 80%的利润 ...

  4. 前端微信小程序仿菜谱精灵

    需求描述及交互分析 设计思路和相关知识点 底部标签导航设计 幻灯片轮播效果设计 菜谱专题列表显示设计 菜谱专题详情设计 菜谱分类设计 幻灯片轮播效果动态切换展示一些美食图片 若本号内容有做得不到位的地 ...

  5. python在windows上创建虚拟环境

    cmd进入 安装virtualenvwrapper-win pip install virtualenvwrapper-win 创建名为pyve的虚拟环境 mkvirtualenv pyve 退出虚拟 ...

  6. Multi-shot Pedestrian Re-identification via Sequential Decision Making

    Multi-shot Pedestrian Re-identification via Sequential Decision Making 2019-07-31 20:33:37 Paper: ht ...

  7. 【深入学习linux】VMware新建虚拟机教程

    新建虚拟机 1. 点击创建新的虚拟机 2.进入新建向导,选择典型安装,点击下一步 3.为什么不选择第二个选项是因为系统会默认帮助我们安装一个简单配置,这里我们需要学习配置安装所以选择第三项,然后点击下 ...

  8. flask 开发接口测试平台

    flask 开发接口测试平台 数据库,forms  views  视图, 数据库如下: # encoding: utf-8 ''' @author: lileilei @file: models.py ...

  9. C# default(T)关键字

    C#关键词default函数,default(T)可以得到该类型的默认值. C#在类初始化时,会给未显示赋值的字段.属性赋上默认值,但是值变量却不会. 值变量可以使用默认构造函数赋值,或者使用defa ...

  10. Docker打包部署前端项目与负载均衡

    设置淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org //在home/fn1 home/fn2放入项目和nginx ...