【HDU - 1022】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. 
  

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.
FINISHFor 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.".

Hint

Hint

解题报告:

     刚开始天真的以为直接翻转数组,如果匹配就YES否则就NO。但是一想这题没这么简单,因为比如:我一共要停4辆车,我可以先停三辆,然后出两辆,然后再进一辆,这样也是符合题意的。

    别忘了这里用了数据结构,所以任何操作都必须判断一步栈是否为空。

AC代码:

#include<bits/stdc++.h>using namespace std;
int opr[1005];
int main()
{char in[15],out[15];int n;while(~scanf("%d",&n)) {int top = 0;scanf("%s%s",in,out);stack<char > sk;int cur=0;for(int i = 0; i<n; i++) {sk.push(in[i]);opr[++top] = 1;if(sk.size()) {while(sk.size() && sk.top() == out[cur]) {cur++;sk.pop();opr[++top] = 0;}} }if(sk.empty()) {puts("Yes.");for(int i = 1; i<=top; i++) {opr[i] == 1 ? puts("in") : puts("out");}}else puts("No.");puts("FINISH");}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441661.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!)

题干&#xff1a; After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty d…

任意阶魔方阵matlab程序,【精品】任意阶魔方阵算法(c语言)

n阶幻方是由前n^2(n的2次方)个自然数组成的一个n阶方阵&#xff0c;其各行、各列及两条对角线所含的n个数的和相等。洛书就是最基本的33阶魔方阵&#xff0c;做出某种最恰当的决定&#xff0c;横竖都有3个格。 0的倒数 a&#xff0d;1可以对于 n 阶单位矩阵 e 以及同阶的方阵 a…

【HDU - 1302】The Snail (模拟,水题)

题干&#xff1a; A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each succe…

悟空php微信复制的东西在哪找,微信收藏的文件在哪?从哪里能看到?

现在的微信有很多的小功能&#xff0c;非常的方便实用&#xff0c;但是很多功能大家都不知道&#xff0c;今天&#xff0c;开淘网小编就来教教大家怎么使用微信的“我的收藏”功能。这个功能非常实用&#xff0c;而且收藏的源文件删除的话&#xff0c;我们从收藏里还是一样能用…

python批量打印机excel,python自动化办公系列03_单个以及批量处理excel文件

先贴上数据集&#xff0c;链接&#xff1a;https://pan.baidu.com/s/1ttv7NwbRmfVPcj2iBHTAfg提取码&#xff1a;zg5v下面是关于如何计算每个销售额以及总销售的代码。import osimport pandas as pdos.chdir("C:\\Users\\yuyuk\\data science\\data analysis and descript…

【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)

题干&#xff1a; In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequ…

redis阻塞队列 java,Redis阻塞/非阻塞队列

非阻塞队列RPUSH key value [value ...]RPOP keyLPUSH key value [value ...]LPOP keyR/LPUSH都是后进先出操作&#xff0c;组合起来则是先进先出&#xff0c;属于非阻塞操作&#xff0c;即&#xff1a;不管有无找到指定键值都立即返回。阻塞队列RPUSH key value [value ...]BR…

*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)

题干&#xff1a; Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends.…

matlab aviobj,MATLAB AVI 视频读取处理

MATLAB AVI 视频读取处理1、用matlab读取avi视频(只能读一定压缩各式的avi 电影&#xff0c;这是因为avi视频文件的编码有很多&#xff0c;而matlab只支持部分编码格式。可见http://www.doczj.com/doc/4ae6c6222af90242a895e5fd.html/IdoIwill/article/details/2125838)aviinfo…

【CodeForces - 745B】Hongcow Solves A Puzzle (思维,乱搞,字符串)

题干&#xff1a; Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character X denote…

升级oracle spu,关于Oracle数据库PSU/SPU/BundlePatch的补丁号变化

思庄中心OCP脱产班1月15日开班&#xff0c;周末班于1月12日开班&#xff01;熟悉 OracleDatabase PSU、Bundle Patch 的DBA一定知道&#xff0c;一般来讲&#xff0c;这些 patch的命名规则一般是按照推出的先后顺序&#xff0c;比如在 2015年7月 推出的对 11.2.0.4 的第7个 DB …

【POJ - 2631】Roads in the North (树的直径,模板)

题干&#xff1a; Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village…

h5前端有php,【前端】为什么有些网站的页面地址,没有加上html或者php或者jsp的后辍名?...

如写成http://www.例子.com/page/help的形式回答因为不需要Every HTTP URL conforms to the syntax of a generic URI. A generic URI is of the form:scheme:[//[user:[email protected]]host[:port]][/]path[?query][#fragment]via Wikipedia可见 URI 并不需要以后缀名结尾。…

【CodeForces - 803D】Magazine Ad(二分答案)

题干&#xff1a; The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this: There are space-separated non-empty words of lowercase and uppercase Latin letters. There are hyphen characters - i…

freeradius mysql php,freeradius mysql的相关配置说明

freeradius和mysql的配置# mysqladmin -u root -p create radius# mysql -u root -p radius#GRANT SELECT ON radius.* TO radiuslocalhost IDENTIFIED BY radpass;mysql> GRANT ALL on radius.radacct TO radiuslocalhost;mysql> GRANT ALL on radius.radpostauth TO ra…

php的mcypt,php 7.3 在Centos6.x下的安装过程

提示unrecognized options: –with-mcrypt, –enable-gd-native-ttf表示php7.3不支持这两个选项&#xff0c;编译准备工作:yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-dev…

【CSU - 1023】【HRBUST - 1039】修路(二分,枚举)

题干&#xff1a; 前段时间&#xff0c;某省发生干旱&#xff0c;B山区的居民缺乏生活用水&#xff0c;现在需要从A城市修一条通往B山区的路。假设有A城市通往B山区的路由m条连续的路段组成&#xff0c;现在将这m条路段承包给n个工程队&#xff08;n ≤ m ≤ 300&#xff09;。…

php继承和重载区别,php继承中方法重载(覆盖)的应用场合

php继承中方法重载(覆盖)的应用场合发布于 2015-02-23 17:57:23 | 91 次阅读 | 评论: 0 | 来源: 网友投递PHP开源脚本语言PHP(外文名: Hypertext Preprocessor&#xff0c;中文名&#xff1a;“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点&am…

【CodeForces - 570A】Elections(模拟,水题)

题干&#xff1a; The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate. The electoral system in the country is pretty unusual. At the first …

linux查看文件从底部开始,linux中怎样从底部向上查看log文件

Leetcode&colon; sliding window maximumAugust 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...用 pyvenv 创建几个不相互影响的python虚拟环境IN MY UBUNTU python2的环…