HDU ACM1022——Train Problem I

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

题目大意:火车1按顺序进栈,同时可以在任意时刻出栈(只有栈顶元素能出栈)。如火车1:1234,1进栈,2进栈,2出栈,3进栈,4进栈,4出栈,3出栈,1出栈,则最终出来结果为2431。我们的目标是使火车1出来的结果与火车2相同。
这是一道关于【栈】的题目,所以我就用栈的方法做了一遍,不过这样可能有些难理解,所以我还用数组模仿栈做了一遍。

用栈

#include<stdio.h>
#define Stack_Size 10002
#define FALSE 0
#define TRUE 1
typedef char StackElementType;  /*定义栈元素类型为int,这样定义方便以后阅读 */ 

typedef struct{
    StackElementType elem[Stack_Size];  /*用来存放栈中元素的一维数组 */ 
    int top;    /*用来存放栈顶元素的下标,top为-1表示空栈*/ 
}SeqStack;

void InitStack(SeqStack *S)
{   /*构造一个空栈S */ 
    S->top =-1;
} 

int Push(SeqStack *S,StackElementType x)
{   /*将x置入S栈新栈顶*/
    if(S->top == Stack_Size-1) return(FALSE); /*栈已满*/
    S->top++;   /*修改栈顶指针*/
    S->elem[S->top]=x;  /* x进栈*/
    return(TRUE); 
} 

int Pop(SeqStack *S,StackElementType *x)
{   /*将S栈顶元素弹出,放到x所指的存储空间中带出*/
    if(S->top==-1) return(FALSE); /*栈为空*/
    *x =S->elem [S->top--]; /*栈顶元素赋值给x并修改栈顶指针*/
    return (TRUE); 
}

int GetTop(SeqStack *S,StackElementType *x)
{   /*将栈S栈顶元素读出,放到x所指的储存空间中,栈顶指针保持不变*/
    if(S->top==-1) return(FALSE); /*栈为空*/
    *x =S->elem [S->top];   /*栈顶元素赋值给x*/
    return (TRUE); 
}

int IsEmpty(SeqStack *S)
{   /*判断栈S是否为空*/ 
    if(S->top == -1)return(TRUE);
    else return (FALSE);
}

int main()
{
    int n,con[20004];
    char str1[10002],str2[10002];

    while(scanf("%d %s %s",&n,str1,str2)!=EOF){
        SeqStack S;StackElementType x;
        InitStack(&S);  /*初始化一个空栈*/ 
        int j=0,i=0,k=0;
        Push(&S,str1[i++]);     /*火车1车头进栈 */ 
        con[k++]=1;     /*记录每次进栈出栈,进栈为1,出栈为2*/ 
        while(!IsEmpty(&S)){
            GetTop(&S,&x);
            if(x==str2[j]){     /*如果栈顶元素与火车2车头相同,栈顶元素出栈 */ 
                Pop(&S,&x);
                con[k++]=2;
                j++;    /*更新火车2车头*/ 
            }
            else if(n>i) {      /*如果不同且火车1未完全进栈,则继续进栈 */ 
                Push(&S,str1[i++]);
                con[k++]=1;
            }
            else if(n==i && x!=str2[j]) break;  /*如果火车1已经完全进栈且栈顶元素
                                                与现火车2车头不同,则不能再改变*/ 
            if(n>i && IsEmpty(&S)){     /*如果火车1未全部进栈而栈已空,则继续进栈*/ 
                Push(&S,str1[i++]);
                con[k++]=1;
            }
        }
        if(j==n){       /*j与n相等代表火车2已经全部匹配过一遍*/ 
            printf("Yes.\n");
            for(i=0;i<k;i++){
                if(con[i]==1)printf("in\n");
                else if(con[i]==2)printf("out\n");
            }
        }
        else printf("No.\n");
        printf("FINISH\n"); 
    }
    return 0; 
}

不用栈

#include<stdio.h>
int main()
{
    int n,con[20004];
    char str1[10002],str2[10002],str3[10002];

    while(scanf("%d %s %s",&n,str1,str2)!=EOF){
        int j=0,i=0,k=0,top=0;
        str3[top++]=str1[i++];      /*火车1车头进栈 */ 
        con[k++]=1;     /*记录每次进栈出栈,进栈为1,出栈为2*/ 
        while(top!=0){
            if(str3[top-1]==str2[j]){       /*如果栈顶元素与火车2车头相同,栈顶元素出栈 */ 
                top--;
                con[k++]=2;
                j++;    /*更新火车2车头*/ 
            }
            else if(n>i) {      /*如果不同且火车1未完全进栈,则继续进栈 */ 
                str3[top++]=str1[i++];  
                con[k++]=1;
            }
            else if(n==i && str3[top-1]!=str2[j]) break;    /*如果火车1已经完全进栈且栈顶元素
                                                            与现火车2车头不同,则不能再改变*/ 
            if(n>i && top==0){      /*如果火车1未全部进栈而栈已空,则继续进栈*/ 
                str3[top++]=str1[i++];  
                con[k++]=1;
            }
        }
        if(j==n){       /*j与n相等代表火车2已经全部匹配过一遍*/ 
            printf("Yes.\n");
            for(i=0;i<k;i++){
                if(con[i]==1)printf("in\n");
                else if(con[i]==2)printf("out\n");
            }
        }
        else printf("No.\n");
        printf("FINISH\n"); 
    }
    return 0; 
}

猜你喜欢

转载自blog.csdn.net/BarisGuo/article/details/82108301