杭电1022 Train Problem I

来源:互联网 发布:知易商标网 编辑:程序博客网 时间:2024/05/21 17:48

英文题。

这是栈的最简单应用

尽管这道题不是我自己编写出来的,但它暂时帮我理解了栈这种数据结构的运用思想。

本题思路:in和out是进站次序和出站次序,flag用于记录进出站序列,sta构建了一个栈的结构,用于存放列车的入栈序列,top是栈顶指针。

如果栈顶和out的首是相同的,则执行出站,且out指向下一个要出站的位置,flag标记进站,如果不相同,则列车继续进站,修改栈顶指针并用flag标记出站。最后如果栈的元素个数超过了n,则所有列车全进站。


Train Problem I

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 3213 123 312

Sample Output

Yes.inininoutoutoutFINISHNo.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.".

#include<stdio.h>char in[100],out[100];//in为车的出发顺序,out为车到达终点的顺序int flag[200];int sta[200];int main(){    int n;    int top;    int i,j,k;    int ok;    while(scanf("%d%s%s",&n,&in,&out)!=EOF)//n为车的数量    {        //scanf("%s%s",&in,&out);        top=0;//表示栈顶指针        i=j=k=0;//i表示要进车站的车,j表示要出车站的车        ok=1;//出得来        while(j<n)        {            if(in[i]==out[j])//判断进的那辆车有没有被堵死,这里进和出是同一辆车            {                flag[k++]=0;//表示车进车站                flag[k++]=1;//表示车出车站                i++;                j++;            }                else if(top>0 && sta[top-1]==out[j])//栈顶指针大于0,且出站的车就是指针要指向的位置            {                flag[k++]=1;                top--;                j++;            }              else if(i<n)//进栈程序段            {                sta[top++]=in[i++];//修改栈顶指针                flag[k++]=0;//并标志为0                            }               else            {                ok=0;//用该变量来标志上述情况不成立,出站失败                break;            }               }            if(ok==0) printf("No.\n");        else        {            printf("Yes.\n");            for(int i=0;i<k;i++)            {                if(flag[i]==0)                   printf("in\n");                else printf("out\n");            }            }            printf("FINISH\n");    }        return 0;}  

编后语:我是个励志的人,别人评价我说天生励志,所以我这个乐天派在这里再送给大家一句话,用于共勉
                  
  • Never give up on what you really want to do. The person with big dreams is more powerful than one with all the facts. 永远不要放弃你真正想要做的事,怀着伟大梦想的人总比接受所有现实的人强大。
0 0
原创粉丝点击