UVA Train Problem I

  • 描述
    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.

在这里插入图片描述在这里插入图片描述在这里插入图片描述

  • 输入
    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.
  • 输出
    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.
  • 样例输入
    3 123 321
    3 123 312
  • 样例输出
    Yes.
    in
    in
    in
    out
    out
    out
    FINISH
    No.
    FINISH
    第一次的时候写时虽然写的很乱但测试很多组数据是对了只不过提交时还是依然错误
#include<iostream>

#include<cstdio>
#include<stack>
using namespace std;
stack <char> st;
char o1[10];
char o2[10];
int main()
{
    int n;
    while(scanf("%d%s%s",&n,&o1,&o2)!=EOF)
  {
     int k=0,sum=0,flag,aflag=1;

         for(int i=0,j=0;i<n*2;i++,j++)
    {
        flag=0;
        if(j<n){st.push(o1[j]);flag++;}
        if(o2[k]==st.top()&&!st.empty()){st.pop();k++;sum++;flag++;}
        if(flag==2) i++;
        if(sum==n) aflag=0;
    }
    if(aflag){printf("No.\n");printf("FINISH\n");}
    else
    {
     k=0;
     printf("Yes.\n");
    for(int i=0,j=0;i<n*2;i++,j++)
     {
        flag=0;
        if(j<n){st.push(o1[j]);printf("in\n");flag++;}
        if(o2[k]==st.top()&&!st.empty()){printf("out\n");k++;st.pop();flag++;}
        if(flag==2) i++;
        if(i==n*2-1) printf("FINISH\n");
     }
    }
  }
  return 0;
}

后来看了看其他大佬的答案发现可以改进的有以下几个地方

  • 可以用一个数组来记录In和out的记录,这样就不用想我之前那样写两遍出栈入栈的操作感觉就是这里出的错
  • 对于一些标志变量的使用尽量不要过多这样容易忘记
  • 忘了在每一次开头清空栈

然后以下是我修改之后的ac答案

#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
stack <char> st;
int main()
{
    int n,j,k,flag[20];
    char o1[10];
    char o2[10];
    while(scanf("%d",&n)!=EOF)
    {

        memset(flag,-1,sizeof(flag));
        while( !st.empty() )
            st.pop();
        cin>>o1>>o2;
        j=k=0;
        for(int i=0;i<n;i++)
        {
            st.push(o1[i]);
            flag[k++]=1;//1用来表示in 0用来表示out
            while(!st.empty()&&st.top()==o2[j])
            {
                flag[k++]=0;
                st.pop();
                ++j;
            }

        }
        if(j==n)
        {
            cout<<"Yes.\n";
            for(int i=0;i<k;++i)
            {
                if(flag[i]) cout<<"in\n";
                else
                    cout<<"out\n";
            }
            cout<<"FINISH\n";
        }
        else
            cout<<"No.\nFINISH\n";
    }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值