zoukankan      html  css  js  c++  java
  • HDU_1022

    题目:

    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
    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 #include <iostream>
     2 #include <stack>
     3 #include <string>
     4 #include <bits/stdc++.h>
     5 using namespace std;
     6 
     7 #define IN 1
     8 #define OUT 0
     9 string a,b;
    10 #define MAX_NUM 100100
    11 
    12 int sep[MAX_NUM];
    13 
    14 int main(int argc, char const *argv[])
    15 {
    16     int n;
    17     while(cin >> n >> a >> b){
    18         stack<char> s;
    19         int i = 0, j = 0, k = 0;
    20         s.push(a[0]);
    21         sep[k++] = IN;
    22         while(i < n && j < n){
    23             if(s.size() != 0 && s.top() == b[j]){
    24                 j++;
    25                 s.pop();
    26                 sep[k++] = OUT;
    27             }else{
    28                 s.push(a[++i]);
    29                 sep[k++] = IN;
    30             }
    31         }
    32         if(i == n){//如果i==n表示栈顶元素不等于序列2当前元素,且序列1中元素都已经入过栈,判断不能得到序列2一样的答案。 
    33             cout << "No." << endl;
    34         }else{
    35             cout << "Yes." << endl;
    36             for (int i = 0; i < k; ++i)
    37             {
    38                 if(sep[i]){
    39                     cout << "in" << endl;
    40                 }
    41                 else{
    42                     cout << "out" << endl;
    43                 }
    44             }
    45         }
    46         cout << "FINISH" << endl;
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    K-邻近算法
    算法
    (12)ubunto 快捷键
    (38)C#IIS
    RichEditControl(富文本控件)
    Gaugecontrol(测量仪器图形控件)
    鏖战字符串
    bzoj3713 [PA2014]Iloczyn|暴力(模拟)
    约会安排HDU
    hdu4614 线段树+二分 插花
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6473179.html
Copyright © 2011-2022 走看看