HDU 1022

简介: Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10829    Accepted Submission(s): 3924 Problem Description As the ne

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10829    Accepted Submission(s): 3924


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.".
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;
int c[10];
char a[10],b[10];
int main()
{
    int n;
    stack<char>S;
    while(scanf("%d",&n)!=EOF)
    {
        int k=0,j=0,i;
        scanf("%s",a);
        scanf("%s",b);
        for(i=0; i<n; i++)
        {
            S.push(a[i]);
            c[k]=1;
            k++;
        while(j<n&&!S.empty())
        {
            if(S.top()==b[j])
            {
                S.pop();
                c[k]=0;
                j++;
                k++;
            }
                else
                break;

        }
      }
            if(S.empty())
            {
                printf("Yes.\n");
                for(i=0; i<k; i++)
                {
                    if(c[i]==0)   printf("out\n");
                    else if(c[i]==1)   printf("in\n");
                }
            }
            else
                printf("No.\n");
            printf("FINISH\n");
            while(!S.empty())
              S.pop();

    }
    return 0;
}


目录
相关文章
|
算法 Java
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
176 0
hdu 1892 See you~
点击打开hdu 1892 思路: 二维树状数组 分析: 1 题目给定4种操作:  S x1 y1 x2 y2 询问以(x1 , y1) - (x2 , y2)为对角线的矩形的面积,但是这个对角线不一定是正对角线。
1018 0
|
存储
hdu 2795 Billboard
点击打开hdu 2795 思路: 线段树+单点更新 分析: 1 题目的意思是给定一个h*w的广告牌h为高,w为宽,现在有n个高为1宽为wi的小广告要放上去,原则是最先放最上面和最左边的位置 2 题目的h和w的最大值为10^9,但是n最大为200000。
768 0
hdu 4463 Outlets
点击打开链接hdu 4463 思路:最小生成树+kruskal 分析: 1 题目要求的找到n个商店组成n-1条边,并且要求耐克和苹果商店肯定要相连,求最短长度 2 很明显的最小生成树的模板题,由于要求耐克和苹果的商店要在一起那么就要把这两个商店编号合并到同一个集合,然后在利用kruskal计算。
909 0
|
Java 机器学习/深度学习
HDU 1285
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6329 Accepted Submission(s): 2393 Proble...
745 0
|
Java
HDU 1799
循环多少次? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1343 Accepted Submission(s): 485 Problem Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分。
819 0