每日算法练习——Train Promblem1(铁轨问题)

题目来源:航电OJ http://acm.hdu.edu.cn/showproblem.php?pid=1022

题目:

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

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

Author

Ignatius.L

翻译:

 分析:

我刚做这道题的没什么思路,都来看了一本书上的解法加以大佬的提示才弄明白的。

输入可以是这样的 4 1234 3214 这是可以实现的 顺序为

in
in
in
out
out
out
in
out
in
out
Yes.
FINISH

这道题的情景可以用栈来模仿,是非常好的栈的练习题。

就用上面的示例来过一遍代码

首先

queue<char> q;    //队列来记录出入顺序
stack<char> s;    //栈来表示C车站的车厢排列情况

然后

用数组A B分别接受进栈顺序和目标出栈顺序。

重点是循环模拟进站的过程:

while判断条件为b<=n 是要循环一边目标出栈顺序

if(A[a]==B[b]) 是目标出栈顺序按照进站顺序进出中转站C,则一直执行该语句直至跳出循环 例如:1234 1234

else if (!s.empty()&&s.top()==B[b]) 是当栈内不为空,且栈顶元素为目标顺序中当前的元素时,将栈顶的元素出栈。

    例如:输出第二个out时 b=1 则B[b]=2 此时 栈s的顶部元素也为2,这样就满足可这个else if 的条件 ,进入语句,out掉2 并且在q中记录一个0,表示出栈,再将b++;

else if (a<=n) 是当前的A[a] B[b]元素不满足上面两种情况时将A[a]压入栈中,等待匹配到一个和它相等B[b]元素时进入

else if (!s.empty()&&s.top()==B[b])语句出栈

else 语句时当不满足上面任意一种情况时执行 ok 置为0 表示,该顺序不能实现。

最后结束循环打印结果

 

实例代码C++

#include <iostream>
#include <stack>
#include <cstdio>
#include <queue>

#define maxn 1000

using namespace std;

int n,a,b;    // a b 为字符串下表,n为字符串长度
char A[maxn],B[maxn];    //A B数组为两个字符串

int main(void)
{

    while(scanf("%d",&n)==1)
    {
        //初始化栈和队列
        queue<char> q;    //队列来记录出入顺序
        stack<char> s;    //栈来表示C车站的车厢排列情况

        // 初始化数据
        a=0,b=0;
        while(!q.empty())
            q.pop();
        while(!s.empty())
            s.pop();
        int ok = 1;    //标记能否实现

        // 输入进栈顺序和目标出栈顺序
        scanf("%s %s",A,B);

        // 模拟出栈入栈
        while(b<=n)
        {
            if(A[a]==B[b])//车厢按顺序进出中转站C,则一直执行该语句直至跳出循环 例如:123 123
            {
                a++;
                b++;
                q.push('1');//in = 1
                q.push('0');//out = 0

            }
            else if (!s.empty()&&s.top()==B[b])//若栈不为空,且栈中车厢和出栈顺序中当前正在匹配的元素相同,则栈中的头车厢可以出栈
            {
                s.pop();
                b++;
                q.push('0');

            }
            else if (a<=n) //车厢入栈等待
            {
                s.push(A[a++]);
                q.push('1');
            }
            else//车厢既不是按顺序,也不是按逆序进出中转站C
            {
                ok=0;
                break;
            }

        }
        // 输出 出入方法
        if(ok==1)
        {
            while(!q.empty())
            {
                printf("%s\n", q.front()=='1' ? "in":"out");
                q.pop();
            }
            printf("Yes.\nFINISH\n");
        }
        else
            printf("No.\nFINISH\n");

            
    }
    return 0;
}

算法小白,有不对的地方,请大佬指出,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值