zoukankan      html  css  js  c++  java
  • 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
    (菜鸟网上档的代码,觉得不错)
    View Code
    //此题不真正的仔细读难理解出事栈的应用,容易想成反序相等就yes的问题,此题题意是说输入的两列字符串,第一列表示进入的顺序,第二列表示出去的顺序,问你是否符合后进先出。
    //举出一列数据 7 1234567 4321576
    //上面数据应该是 in in in in out out out out in out in in out out;
    C++代码如下:
    #include<iostream>
    #include<cstdlib>
    #include<string>
    #include<cstring>
    using namespace std;
    const int Stacksize=100;
    const int INCREMENT=50;
    typedef struct
    {
        char *top;
        char *base;
        int stacksize;
    }Stack;
    int main()
    {
        int a[1000];
        int i,k,count;
        Stack S;
        int n;
        string s1,s2;
        while(cin>>n>>s1>>s2)
        {
            memset(a,0,sizeof(a));
            S.base=(char*)malloc(Stacksize*sizeof(char));
            if(!S.base)
            return 0;
            S.top=S.base;
            S.stacksize=Stacksize;
            i=k=count=0;
            while(k<n)
            {
                if(S.top-S.base>=S.stacksize)
                {
                    S.base=(char *)malloc((S.stacksize+INCREMENT)*sizeof(char));
                    if(!S.base)
                    return 0;
                    S.top=S.base+S.stacksize;
                    S.stacksize+=INCREMENT;
                }
                if(S.base==S.top)//如栈为空,进栈
                {
                    *S.top=s1[i];
                    a[count++]=1;
                    i++;
                    S.top++;
                }
                else
                {
                    if(*(S.top-1)==s2[k])//出栈
                    {
                        S.top--;
                        k++;
                        count++;
                    }
                    else
                    {
                        if(i==n)//如果已经i已经等于n了表示结束
                        break;
                        *S.top=s1[i];//else继续进栈
                        S.top++;
                        i++;
                        a[count++]=1;
                    }
                }
            }
            if(S.top==S.base)
            {
                cout<<"Yes.\n";
                for(i=0;i<count;i++)
                {
                    if(a[i])
                    cout<<"in\n";
                    else
                    cout<<"out\n";
                }
                cout<<"FINISH\n";
            }
            else
            cout<<"No.\nFINISH\n";
            free(S.base);
        }
        return 0;
    }
  • 相关阅读:
    【HDU2222】Keywords Search(AC自动机)
    -网络流题表
    【 POJ
    【 UVALive
    【POJ2699】The Maximum Number of Strong Kings(网络流)
    【UVALive
    【HDU3081】Marriage Match II (二分+最大流)
    【UVALive
    【LA2796】Concert Hall Scheduling(最大费用最大流)
    【 UVALive
  • 原文地址:https://www.cnblogs.com/zlyblog/p/3037159.html
Copyright © 2011-2022 走看看