现在的位置: 首页 > 综合 > 正文

杭电ACM 1022–Train Problem

2018年06月06日 ⁄ 综合 ⁄ 共 2341字 ⁄ 字号 评论关闭

Problem Description:

As the new term comes, the Ignatius Train Station isverybusy 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.


题目的意思就是给定列车的进站和出站顺序,判断能否由进站顺序得出出站顺序。车站为“堆栈”结构,只能先进后出。


此题只要理清关系并不难。逻辑顺序可大致由以下流程图组成:

in为列车入站顺序数组,out为列车出站顺序数组,jin为第j个入站列车,jout为第j个出站列车,n为列车数,s为车站堆栈

下面为c++代码:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct
{
    char* base;
    char* top;
    int stacksize;
}MyStack;

void InitStack(MyStack &s,int size)
{
    s.base=(char *)malloc(size*sizeof(char));
    if(!s.base)exit(0);
    s.top=s.base;
    s.stacksize=size;
}

char GetTop(MyStack s)
{
    char a;
    if(s.top==s.base)return 0;
    s.top--;
    a=*(s.top);
    return a;
}

int Push(MyStack &s,char a)
{
    *(s.top)=a;
       (s.top)++;
    return 1;
}

int Pop(MyStack &s)
{
    (s.top)--;
    return 1;
}

int main()
{
    char in[10],out[10],a,jin,jout,printout;
    char *output[20];
    int n,i;
    while(cin>>n)
    {
        for(i=0;i<n;i++)
        {
            cin>>in[i];
        }

        for(i=0;i<n;i++)
        {
          cin>>out[i];
        }

       MyStack s;
       InitStack(s,10);

           jin=0;
           jout=0;
           printout=0;

unequal:
           if(jin>=n)goto Printout;
           Push(s,in[jin]);
            jin++;
            output[printout]="in";
            printout++;

equal:
            if(out[jout]==GetTop(s)&&jout<n)
            {
                Pop(s);
                output[printout]="out";
                printout++;
                jout++;
                goto equal;
            }
            else if(jout>=n)goto Printout;
            else goto unequal;

Printout:
            if(printout==2*n)
            {
                cout<<"Yes."<<endl;
                for(i=0;i<printout;i++)
                {
                    cout<<output[i]<<endl;
                }
            }
            else cout<<"No."<<endl;

            cout<<"FINISH"<<endl;

    }
    return 0;
}







抱歉!评论已关闭.