zoukankan      html  css  js  c++  java
  • [ACM] hdu 1022 Train Problem I(栈的使用)

    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

    解题思路:

    火车后进站的先出来,这和栈的特性一样,符合后进先出,题目中给出两个编号串,如第一组数据的123 代表编号1先进站,然后2进站,3进站,出站串321,可以让3先出站,然后2,最后1,可以按出站串那样的顺序完全出站。而第二组数据进站串为123,出站串为312,先看出站串,第一个是3,首先得保证3已经进站,按照进站串的顺序1进站,2进站,3进站,然后3出站,再看出站串的第二个数1,要求1出站,但是此时站里面2是比1后进站的,在2没有出站的情况下1不能出站,因此不能按照出站串那样的顺序出站。

    代码:

    #include <iostream>
    #include <stack>
    #include <string.h>
    using namespace std;
    int n;string in,out;
    int flag[25];//用来保存火车的进出站状态
    
    int main()
    {
        while(cin>>n)
        {
            cin>>in>>out;
            int len=in.length();
            stack<char>s;
            int c=0;
            int j=0;//出站串的第一个火车编号
            for(int i=0;i<len;i++)
            {
                s.push(in[i]);//当前火车入站
                flag[c++]=1;//1代表入站
                while(!s.empty()&&s.top()==out[j])//当站里面有火车且当栈顶火车编号符合出站的第一个编号时
                {
                    s.pop();//出站
                    flag[c++]=0;//0代表出站
                    j++;//出站串的火车编号后移
                }
            }
            if(s.empty())//当入站的火车能够完全按照出站串的顺序出站的时候,意味着栈中的元素为空
               {
                   cout<<"Yes."<<endl;
                   for(int i=0;i<c;i++)//输出火车进出站的状态
                    if(flag[i]==1)
                    cout<<"in"<<endl;
                   else
                    cout<<"out"<<endl;
                   cout<<"FINISH"<<endl;
               }
            else//不能完全出站
               {
                   cout<<"No."<<endl;
                   cout<<"FINISH"<<endl;
               }
    
        }
        return 0;
    }
    


     

  • 相关阅读:
    linux 下源码编译环境配置
    CentOS服务器初始化设置
    CentOS-7系统安装配置
    .Net Core 商城微服务项目系列(三):Ocelot网关接入Grafana监控
    .Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关
    .Net Core 商城微服务项目系列(一):使用IdentityServer4构建基础登录验证
    Docker系列(一):容器监控工具Weave Scope安装
    eShopOnContainers学习系列(二):数据库连接健康检查
    eShopOnContainers学习系列(一):Swagger的使用
    ELK系列(二):.net core中使用ELK
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697784.html
Copyright © 2011-2022 走看看