传送门

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. Tomcat 启动提示未发现 APR 的解决方法

    Tomcat 启动出现信息如下: 信息: The APR based Apache Tomcat Native library which allows optimal performance in ...

  2. Android反编译工具的使用-Android Killer

    今天百度搜索“Android反编译”搜索出来的结果大多数都是比较传统的教程.刚接触反编译的时候,我也是从这些教程慢慢学起的.在后来的学习过程中,我接触到比较方便操作的Android反编译.在这,我将使 ...

  3. .Net分布式异常报警系统-服务端Service

    服务端的2个Service 1. HandleService: 从Redis中获取异常信息, 入库并发送通知到相关责任人.  2. HealthyCheckService: 对站点指定页面进行模拟访问 ...

  4. SQL Server Data Tools – Business Intelligence for Visual Studio 2012安装时提示“The CPU architecture....”的解决方法

    SQL Server Data Tools – Business Intelligence for Visual Studio 2012,一个很强大的工具,下载地址:http://www.micros ...

  5. Windows Phone 8 下载文件进度

    后台代码: public partial class MainPage : PhoneApplicationPage { private long siz; private long speed; p ...

  6. [BZOJ2257][Jsoi2009]瓶子和燃料(数学)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2257 分析: 1.先考虑确定的瓶子下的最小体积是多少 ①假设只有两个瓶子v1,v2,易 ...

  7. oracle从游标批量提取数据

    来源于:http://blog.csdn.net/ceclar123/article/details/7974973 传统的fetch into一次只能取得一条数据,使用fetch bulk coll ...

  8. Qt学习中遇到的问题

    问题: 一个Qt小项目,编译成功并成功运行,但应用程序输出中出现如下异常:FTH: (9892): *** Fault tolerant heap shim applied to current pr ...

  9. Question2Answer安装

    Question2Answer安装 Question2Answer的安装过程很简单,只需要几分钟的时间你就可以有一个强大的问答系统. 安装要求 Web服务器(比如Apache) PHP 4.3 或更高 ...

  10. easyui-datagrid 的loader属性用法

    API介绍比较简略: 定义如何从远程服务器加载数据.返回false可以放弃本次请求动作.该函数接受以下参数:param:参数对象传递给远程服务器.success(data):当检索数据成功的时候会调用 ...