hdu1022 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
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.".
 
根据题意很容易得出这样一个结论:火车进站和出站符合“先进后出”的规律。“先进后出”是栈的典型特点。所以首先想到的就是用栈的原理来解决该题。
我们把重点放在代码的实现上:
1.我们先定义两个数组in[ ]和out[ ],分别用来存储进站的火车编号和出站的火车编号。同时设置两个类似于“指针”的变量i, j,分别指向in数组和out数组的首元素。
2.我们先考虑进栈。当栈空或者即将进栈的元素(即i所指向的in数组内的元素)和当前j所指向的out数组的元素不相等时,我们就让i指向的in数组内的那个元素进栈(由于题的特点,每辆火车的编号都具有唯一性,即:栈内的元素注定彼此不同。因此,这种办法在本题中才行得通)。
3.栈顶元素和当前j所指向的out的元素相等的时候,说明火车可以出战,我们就让这个栈顶元素出栈,同时j指向out数组的下一个元素。否则就停止进栈、出栈的操作(因为栈顶元素一旦与当前j所指向的out元素的值不相等,说明火车在车站里面被堵住了,出不来了)。
4.依照上面的步骤。当进栈出栈操作结束以后,若栈里面还有元素,说明火车出不了站了。
5.若栈空的话,则说明火车已经全部出站。那么就只剩一个问题:如何能将in和out指示给打印出来!我们可以设置一个flag数组,当进栈的时候,我们把值设置成1,若出栈的话,我们就把它设置为0,到时候根据flag元素是1还是0,把in和out打印出来。

代码:(我这里flag习惯性的设置成了bool型)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
using namespace std;

int main()
{
    int n,i,j,tag;
    char in[10];
    char out[10];
    bool flag[20];
    while(cin>>n)
    {
        scanf("%s",in);
        scanf("%s",out);
        i = j = tag = 0;
        stack<char> s;
        while(j<n)
        {
            if(s.empty()||s.top()!=out[j]&&i<n)
            {
                s.push(in[i]);
                flag[tag++] = true;
                i++;
            }
            else
            {
                if(s.top()==out[j])
                {
                    s.pop();
                    flag[tag++] = false;
                    j++;
                }
                else
                {
                    break;
                }
            }
        }
        if(s.empty())
        {
            cout<<"Yes."<<endl;
            for(int i=0;i<tag;i++)
            {
                if(flag[i])
                {
                    cout<<"in"<<endl;
                }
                else
                {
                    cout<<"out"<<endl;
                }
            }
            cout<<"FINISH"<<endl;
        }
        else
        {
            cout<<"No."<<endl;
            cout<<"FINISH"<<endl;
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值