传送门

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. 【JavaEE企业应用实战学习记录】struts国际化

    <%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 16:26 To change this tem ...

  2. ElasticSearch入门系列(三)文档,索引,搜索和聚合

    一.文档 在实际使用中的对象往往拥有复杂的数据结构 Elasticsearch是面向文档的,这意味着他可以存储整个对象或文档,然而他不仅仅是存储,还会索引每个文档的内容使之可以被搜索,在Elastic ...

  3. 把时间转成适合符合日常习惯的格式【js】

    假设现在是7月30日12点,我们可以说今天12点,意思也非常明确. 我们习惯说昨天12点,而不习惯说29号12点. 我们习惯说周一12点,而不习惯说28号12点,这样不用翻日历看今天是几号. so,上 ...

  4. ViewHolder数据错乱BUG

    需求是这样的,在列表中用一个图标标示某个item是已经被接下或者完成的任务. 对于文件有这样的操作,进入列表后第一页面展示正常,但是加载更多后同样位置出现了同样的标志.这不是我想要的效果 我的解决办法 ...

  5. Beta版本冲刺———第二天

    会议照片: 项目燃尽图: 1.项目进展: 昨天的困难:分数排行榜的设计 今天解决的进度:完成了界面优化以及建立新的排行榜选项卡界面. 明天要做的事情:分数排行榜的功能设计 2.每个人每天做的事情 郭怡 ...

  6. PHP之function_handling 函数

    function_handling 函数 function_handling 函数 需求: 编写一个函数,传入的参数个数不确定,请求出其和. 使用到 以下几个函数: 代码说明: 函数的参数可以是另外一 ...

  7. CSS选择器优先级排列

    CSS选择器的效率从高到低做了一个排序: 1.id选择器(#myid) 2.类选择器(.myclassname) 3.标签选择器(div,h1,p) 4.相邻选择器(h1+p) 5.子选择器(ul & ...

  8. 读取Properties键值对

    public class CommonFunc { /** * 取properties文件中的键值对 */ public static String getProperties(String para ...

  9. 【转】JSP中文乱码问题终极解决方案

    原文地址:http://blog.csdn.net/beijiguangyong/article/details/7414247 在介绍方法之前我们首先应该清楚具体的问题有哪些,笔者在本博客当中论述的 ...

  10. HoG

    实现步骤 先计算每一个像素点位置上x和y方向上的梯度. 这样在每一个像素点位置上得到一个二维向量, 计算它的方向和模长 将图片分为一个个的cell, 如\(8\times 8\). 计算它的HOG: ...