传送门

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<cstdio>
#include<stack>
using namespace std;
const int maxn = 15;
int main()
{
    int N;
    while (~scanf("%d",&N))
    {
        bool flag = true;
        stack<int>stk;
        int i,inout[maxn] = {0},in[maxn],out[maxn];
        char sin[maxn],sout[maxn];
        scanf("%s %s",sin,sout);
        for (i = 0;i < N;i++)    in[i] = sin[i] - '0';
        for (i = 0;i < N;i++)    out[i] = sout[i] - '0';
    //    for (i = 0;i < N;i++)    printf("%d %d\n",in[i],out[i]);
        int j = 0,p = 0;
        i = 0;
        for (i = 0;i < N;i++)
        {
            if (in[i] != out[j])
            {
                inout[p++] = 0;
                stk.push(in[i]);
            }
            else if (in[i] == out[j])
            {
                inout[p++] = 0;
                j++;
                inout[p++] = 1;
                while (!stk.empty() && stk.top() == out[j])
                {
                    j++;
                    inout[p++] = 1;
                    stk.pop();
                }
            }
        }
        while (!stk.empty())
        {
            if (stk.top() != out[j])
            {
                flag = false;
                break;
            }
            else
            {
                j++;
                stk.pop();
                inout[p++] = 1;
            }
        }
        if (!flag)    printf("No.\nFINISH\n");
        else
        {
            printf("Yes.\n");
            for (i = 0;i < p;i++)    inout[i]?printf("out\n"):printf("in\n");
            printf("FINISH\n");
        }
    }
    return 0;
}

/*
5
12342 24321
*/

  

HDU 1022 Train Problem I(栈模拟)的更多相关文章

  1. hdu 1022 Train Problem I【模拟出入栈】

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

  2. Hdu 1022 Train Problem I 栈

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

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

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

  4. HDU - 1022 Train Problem I STL 压栈

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

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

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

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

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

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

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

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

  9. HDU 1022 Train Problem I

    A - Train Problem I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. LINQ的高级应用

    ---恢复内容开始--- 本文不想罗列linq的通俗使用方法.因为很多博文都已经写得很详细了. 此处直接贴出源码,如果有需要的朋友可以参考,希望更多的朋友能够补充更多的linq的高级应用. 源码如下: ...

  2. C#微信公众号开发系列教程五(接收事件推送与消息排重)

    微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ...

  3. 实用工具 : Xaml Power Toys

    最近挺忙, 憋了一肚子的东西没有分享. 今天分享一个 Xamarin.Forms / WPF 的增强工具 , Visual Studio 扩展 : Xaml Power Toy 可以直接在 VS201 ...

  4. 《深入理解Spark:核心思想与源码分析》(第2章)

    <深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...

  5. Resharper快捷键

    建议你使用 Reshaper 的快捷键,不要担心 Reshaper 会把你原来的快捷键设置给覆盖了,因为如果某个快捷键和 VS 是冲突的,Reshaper会让你自己选择需要使用 VS 还是 Resha ...

  6. centos 服务器内存管理

    du su /目录/ 查看改目录大小 ls -lht /  查看文件详情,显示文件大小(直观) df -h 查看系统内存占用情况

  7. 使用ContentProvider进行应用程序间的数据交互

    什么是ContentProvider: ContentProvider用来管理数据的访问规则.它允许你的应用程序向外界暴露需要被访问的数据. 是Android的四大组件之一. ContentProvi ...

  8. 跨浏览器图像灰度(grayscale)解决方案

    <style type="text/css"> .gray { -webkit-filter: grayscale(100%); /* CSS3 filter方式,we ...

  9. sqlite之聚合函数的使用

    聚合函数对一组值执行计算并返回单一的值.聚合函数对一组值执行计算,并返回单个值.除了 COUNT 以外,聚合函数都会忽略空值. 聚合函数经常与 SELECT 语句的 GROUP BY 子句一起使用. ...

  10. springMVC自定义注解实现用户行为验证

    最近在进行项目开发的时候需要对接口做Session验证 1.自定义一个注解@AuthCheckAnnotation @Documented @Target(ElementType.METHOD) @I ...