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

  • Home
  • Problems
  • My Status
  • Standing
<->
     
 
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 }                                 

 

转载于:https://www.cnblogs.com/xieyizun-sysu-programmer/p/3456801.html

sicily 1022. Train Problem相关推荐

  1. 【HDOJ】1022 Train Problem I_天涯浪子_新浪博客

    [题目]http://acm.hdu.edu.cn/showproblem.php?pid=1022 [报告] 模拟,直接模拟一个栈的运行就行了. [程序] // Task: 1022 Train P ...

  2. 【ACM】杭电1022:Train Problem I

    分析: 明显是一个栈的问题.利用栈后进先出的特点模拟火车进站出站的过程即可轻松解决. 我的思路是: 用2个字符数组保存火车车厢的序列.首先比较出站后(记为s2)数组和出站前 (记为s1)数组的第一个元 ...

  3. HDU 1022[Train Problem I] 栈的应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意:一列序列为s1的车厢是否能通过车站的中转以序列s2出站.白皮上有. 关键思想:栈的应用 ...

  4. hdu 1022 Train Problem I 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 其实是一道上数据结构课讲过的例题  只不过当时讲的是怎么用手算  一道水题又花了好长时间 其实就 ...

  5. hdu 1022 Train Problem I(栈)

    标记现在已经匹配到o1的第几列车了,o2从头开始,如果不匹配把o1就放入栈中,匹配后出栈并比较栈顶与o1的下一辆,匹配继续出,不匹配就继续进栈 1 #include <iostream> ...

  6. HDU 1022 Train Problem I stack 基础题

    题意: 有一个火车站,只有一个口同时用于进入和出去,但不能有2辆或以上同时进出. 给出一个n代表有n辆火车(n<=9),编号分别为1到n. 然后给出2个序列,分别代表火车进站的顺序,火车出站的顺 ...

  7. train problem I (栈水题)

    杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...

  8. Train Problem II(卡特兰数 组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...

  9. Train Problem I(火车进站问题)hdu1022

    问题 Train Problem I - http://acm.hdu.edu.cn/showproblem.php?pid=1022 分析 堆栈的核心逻辑就是"先进后出" 或 & ...

  10. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

最新文章

  1. poj1753Flip Game(dfs)
  2. python 多进程 multiprocessing 进程池pool报错 in join assert self._state in (CLOSE, TERMINATE) AssertionError
  3. ASP.NET CORE 微服务(简化版)实战系列-没有比这性价比再高的实战课程了
  4. 利用对象存储多种方式 保障OSS数据安全
  5. myeclipse创建项目 我们的第一个代码
  6. catia圆柱转化为圆台_中考难点,最值问题之构造与转化
  7. (15)VHDL测试激励编写(复位)
  8. Spring 的 BeanPostProcessor接口实现
  9. linux下查找文件、排序、查看文件内容
  10. html 字体设置草体,CSS 网页安全字体
  11. 百度直连+cns完美,那怎么搭建CNS配合百度直连呢
  12. 16.集合框架(ArrayList,Vector,LinkedList,泛型(Generic),可变参数,增强for循环)
  13. Random Projection 随机投影法
  14. Error:(63, 24) 错误: 枚举 switch case 标签必须为枚举常量的非限定名称
  15. 即构科技肖传发:即刻构建在线教育的音视频互动场景
  16. php周签到功能思路,thinkphp签到功能实现方法
  17. VBS msgbox的参数的用法
  18. 阿哲---正式入住CSDN
  19. Kotlin写界面时诸多控件的点击事件
  20. 自动驾驶行业观察之2023上海车展-----车企发展趋势(3)

热门文章

  1. java中连接数据库
  2. STL标准模版库---stack操作大全
  3. markdown格式的文章如何转换为可以发布在微信公众号上的内容
  4. c语言数组字节偏移,C语言数组中的地址偏移问题
  5. java.util.ArrayList#add探索
  6. 【渝粤教育】国家开放大学2018年秋季 0727-22T思想道德修养与法律基础 参考试题
  7. 【渝粤教育】国家开放大学2019年春季 2718动物生理基础 参考试题
  8. Ridge regression
  9. /usr/bin/ld: 找不到 -lglut
  10. Texar安装、Textgenrnn安装