HDUOJ 1022 Train Problem I (栈的应用)

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 41257    Accepted Submission(s): 15437

 

 

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

FINISH

out 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.

So we output "No.".

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.

题目翻译:

随着新的学期到来,伊格内修斯火车站最近非常繁忙。?很多学生想坐火车回到学校(因为伊格内修斯火车站的火车是全世界最快的)。?但是这里出现了问题,这个火车站只有一条铁路能让所有的火车停下来。?所以所有的列车都只能从一个方向进来,再从刚才进来的方向出去。?对于这个问题,如果列车A首先进入铁路,然后列车B在列车A离开之前进入铁路,则列车A不能离开,直到列车B离开。?下面的图片找能解释题目中所阐述的问题。?现在的问题是,车站最多有9列,所有列车都有一个ID(编号从1到n),列车按照“O1顺序”(就是题目定的一种顺序)进入铁路,你的任务是确定列车能否在“顺序O2”的顺序中出去。?

输入:

输入包含几个测试用例。 每个测试案例由一个整数,列车数量和两个字符串组成,列车进火车站顺序为:O1,列车出火车站顺序为O2。 输入到文件结尾终止。 示例输入中的更多详细信息。

输出:

如果你不能交换01,02的顺序,输出包含一个字符串“No.”,否则你应该输出一行“Yes.”,然后输出你的方式交换顺序(你应该输出“in.”代表一列火车进入铁路,“out.” 代表一列火车离开铁路)。 每个测试用例后打印一行含有“FINISH”。 示例输出中的更多细节。   

提示:

对于第一个样本输入,我们让列车1进入,然后列车2和列车3。   所以现在火车3在铁路的顶端,所以火车3可以先走,然后是火车2和火车1.在第二个样本输入中,我们应该让列车3首先离开,所以我们必须让列车1进入,然后列车2和列车3。  现在我们可以让列车3离开。  但之后我们不能让火车1在火车2之前离开,因为火车2此刻在火车的顶端。所以我们输出“No.”。  

题目解析:火车是按照“顺序1”进来的,你只需判断火车能不能按照“顺序2”出去。并且把这个过程中(包括进火车站跟出火车站)的操作打印出来。注意!!只有一条铁路停火车,那这个问题是不是就变得简单了呢!这就是一个简单的栈应用判断字符串中的字符是否相等。

 

 

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
int main()
{
    stack<char>mystack;
    char s1[10];
    char s2[10];
    int flag[10];
    int i;
    int n;
    int num;
    int ans;
    while(~scanf("%d%s%s",&n,s1,s2))
    {
        while(!mystack.empty())
        {
            mystack.pop();
        }//对栈进行清空
        memset(flag,0,sizeof(flag));
        //对数组进行清空
        num=0;
        ans=0;
        for(i=0; i<n; i++) //循环将数据入栈
        {
            mystack.push(s1[i]);
            flag[num++]=1;//记录操作,1代表入栈,后期用来逻辑判断
            while(!mystack.empty() && mystack.top()==s2[ans])
            {
                flag[num++]=0;//记录操作,0代表出栈
                mystack.pop(); //出栈
                ans++; //让栈顶元素跟字符串s2下一位字符进行比较
            }
        }

        if(ans==n && mystack.empty()) //当这个条件满足说明n长度字符串的比较满足题目
                                      //非常注意这时候栈要空这个条件!!!!
        {
            printf("Yes.\n");
            for(i=0; i<num; i++)  //用一个循环将操作打印出来。
            {
                if(flag[i])
                {
                    printf("in.\n");
                }
                else
                {
                    printf("out.\n");
                }
            }
            printf("FINISH\n");
        }
        else
        {
            printf("No.\nFINISH\n");
        }

    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值