传送门

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. HDU2444-The Accomodation of Students-判断是否为二分图+ISAP

    要先判断是不是二分图.用黑白染色法. 遇到已经染过的跟当前的颜色相同时就说明不是二分图,也即出现了奇环 /*---------------------------------------------- ...

  2. Qt中的qreal

    在桌面操作系统中(比如Windows, XNix等)qreal其实就是double类型:而在嵌入设备系统中,qreal则等同于float 类型.

  3. WPF ListView和ListBox等双击事件问题

    上两篇文章中说双击行获取不到当前数据对象问题, http://www.cnblogs.com/ligl/p/5636899.html http://www.cnblogs.com/ligl/p/562 ...

  4. Android实现滑动刻度尺效果,选择身高体重和生日

    刻度尺效果虽然看起来很美,我个人认为很不实用,即使再不实用,也有用的,鉴于群里成员对我的苦苦哀求,我就分享一个他用不到的,横屏滑动刻度尺,因为他需要竖屏的,哈哈…… 最近群里的开发人员咨询怎样实现刻度 ...

  5. C#:异步编程和线程的使用(.NET 4.5 )

    摘自:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N(葡萄城 ...

  6. The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path。

    项目上右键-->Build Path-->Configuration Build Path -->Add Library -->Server Runtime 选择tomcat

  7. cxf和spring结合,发布restFull风格的服务

    rest(Representational State Transfer):表现层状态转化,它是一种风格,用于资源定位,例如:http://ip:port/user/student/001 和资源操作 ...

  8. Timer定时任务

    // main方法 public static void main(String[] args) { timerEnter(); } // 设定指定任务task在指定延迟delay后进行固定延迟per ...

  9. 【UOJ #20】【NOIP 2014】解方程

    http://uoj.ac/problem/20 并不会做...然后看题解....... 对a取模,避免了高精度带来的复杂度,然后再枚举x判断是否满足模意义下等于0. 取5个模数,我直接抄的别人的_( ...

  10. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...