zoukankan      html  css  js  c++  java
  • sicily 1022. Train Problem

    本题主要是出栈入栈顺序的问题

         
     
     
    1022. Train Problem
     
     
    Total: 2206 Accepted: 703
     
         
         
     
    Time Limit: 1sec    Memory Limit:256MB
    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
     Copy sample input to clipboard
    3 123 321
    3 123 312
    
    Sample Output
    Yes.
    in
    in
    in
    out
    out
    out
    FINISH
    No.
    FINISH
    

    Problem Source: 2012年期末机考(Pan)

     
         
    代码:
     1 // Problem#: 8670
     2 // Submission#: 2493920
     3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
     4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
     5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
     6 #include<iostream>
     7 #include<stack>
     8 #include<string>
     9 #include<vector>
    10 #include<stdio.h>
    11 using namespace std;
    12 int main() {
    13     string in_ = "in";
    14     string out_ = "out";
    15     string finish = "FINISH";   
    16     string in;
    17     string out;
    18     int trains;
    19     
    20     while (scanf("%d", &trains) != EOF) {
    21           cin >> in >> out;
    22           stack<char> station;
    23           vector<string> state;
    24           for (int i = 0; i < in.size(); i++) {
    25               if (in[i] == out[0]) {
    26                   state.push_back(in_);
    27                   state.push_back(out_);
    28                   out.erase(out.begin());
    29                   while (!station.empty()) {//关键一步
    30                        if (station.top() == out[0]) {
    31                            state.push_back(out_);
    32                            out.erase(out.begin());
    33                            station.pop();
    34                        } else
    35                           break;
    36                   }                
    37               } else {
    38                   station.push(in[i]);
    39                   state.push_back(in_);
    40               }
    41           }
    42           state.push_back(finish);
    43           if (station.empty()) {
    44               cout << "Yes." << endl;
    45               for (int i = 0; i < state.size(); i++)
    46                    cout << state[i] << endl;
    47           } else
    48               cout << "No." << endl << finish << endl;
    49      }
    50      return 0;
    51 }                                 

     
  • 相关阅读:
    20 行代码:Serverless 架构下用 Python 轻松搞定图像分类
    Serverless 的内存配置与超时时间
    Serverless 架构与事件规范
    如何用 Serverless 优雅地给网站图片加水印
    修改rpmbuild构建目录的位置
    rpmbuild之构建目录结构解析
    全量编译与增量编译
    c堆排序的实现
    c优先队列的实现
    c栈的实现
  • 原文地址:https://www.cnblogs.com/xieyizun-sysu-programmer/p/3456801.html
Copyright © 2011-2022 走看看