【杭电-oj】-1022-Train Problem I (栈,好)

杭电oj2023题题目详解(Java版) 杭电oj2023题详解 阅读详情

Train Problem I

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


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

看的师父的题解,讲真师父好聪明啊!!! 看了感觉思路清楚极了。

1.这道题操作只有两种,进栈和出栈,如果待入栈火车和最顶目标相同的话,那就进入、出去两个连续的步骤。

2.如果不同,那就考虑栈顶元素和最顶目标是否相同,如果相同,则栈顶元素出栈。

3.如果进行到这一步,就表示这一轮没有火车可以出栈了,则压入一节火车,进行下一轮对比。但是这里要注意思考:是否还有待入栈火车,如果没有的话,则gameover,标记并跳出循环。

上面的过程记录一下,最后分情况输出就行啦。


#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	char a[10],b[10];
	int op[20],num,z;
	int flag;
	while(~scanf("%d",&n))
	{
		num=1;
		z=0;
		flag=1;
		getchar();
		scanf("%s %s",a,b);
		stack<char>sk;
		for(int i=0;i<n;i++)
		{
			if(a[z]==b[i])				//待入元素a[i]和目标元素b[i]相同 直接进入再出去 
			{
				op[num++]=1;			//进入记为1,出记为0 
				op[num++]=0;
				z++;
			}
			else if(!sk.empty()&&b[i]==sk.top())	//如果栈里不为空,且目标元素b[i]与栈顶元素相同则弹出栈顶元素 
			{
				sk.pop();
				op[num++]=0;					//出记为0 
			}
			else if(z<=n)				//前面两种情况都不符合且a中有没有进栈的,就进入 
			{
				sk.push(a[z++]);		
				op[num++]=1;
				i--;					//此时b不动,则i应该减一,恢复原值 
			}
			else
			{
				flag=0;					//全部不符合则不可能 
				break;
			}
		}
		if(!flag)
			printf("No.\nFINISH\n");
		else
		{
			printf("Yes.\n");
			for(int i=1;i<num;i++)			//注意操作的num从1开始记录 
			{
				if(op[i]==1)
					printf("in\n");
				else
					printf("out\n");
			}
			printf("FINISH\n");
		}
	}
}


杭电OJ1000-1099题目答案:编程竞赛知识宝典 本文还有配套的精品资源,点击获取 简介:杭州电子科技大学的在线判题系统(OJ)是编程技能提升的重要平台。该压缩包提供了1000至1099题目的答案,涵盖了基础算法、数据结构、字符串处理、数学思维、递归与回溯、贪心策略、模拟、编码技巧、调试与测试及阅读理解等编程竞赛核心知识点。通过这些答案,学习者不仅可以对照自己的解题思路,还可以深刻理解题目的解题过程,从而全面提高编程和算法... 阅读详情

相关推荐

从零到一:uniPush2.0全链路集成与多厂商通道配置实战

本文提供了uniPush2.0从零开始的全链路集成与多厂商通道配置实战指南。详细讲解了DCloud平台配置、uniCloud云函数开发、客户端CID获取与消息监听,并重点攻克了华为、小米、OPPO、vivo等主流厂商推送通道的配置难点与避坑技巧,帮助UniApp开发者实现“一次集成,全端覆盖”的高效消息推送方案。

onion的博客 437

杭电OJ刷题指南(ACM)

1.首先,打开百度,输入“杭电OJ”进行搜索 2.然后进入杭电OJ官网,进行注册 3.输入注册信息 4.接下来进入详细注册界面,将信息填写完整后点击Submit 5.接下来即可登录帐号,填写用户名和密码后 点击Sign in 6.接下来,进入刷题界面 7.英文题自行翻译,从11页开始有中文题,无论中英完成后都计入刷题数 8.做题时一定要仔细看题,积极尝试 9. 选择答题语言为C语言...

qq_38769551的博客 9万+

电力抄表协议DL/T645和698.45到底有啥区别?一张图帮你选对协议

本文深入对比了电力抄表协议DL/T645和DL/T698.45的技术差异,从协议架构、报文结构、功能覆盖到实施成本进行全面分析。通过实际案例和数据,揭示698.45协议在通信效率、业务扩展性和长期成本上的优势,并提供清晰的选型决策流程图,帮助工程师根据具体需求选择最适合的电力抄表协议。

weixin_38168696的博客 413

杭 电 oj

输入数据有多组,每组占一行,有三个字符组成,之间无空格。对于每组输入数据,输出一行,字符中间用一个空格分开。码从小到大的顺序输出这三个字符。输入三个字符后,按各字符的。

2301_76798724的博客 875

基于PI双闭环解耦控制的三相SVPWM电压型逆变器(3)--调制方法

阐述SPWM和SVPWM的原理以及实现方法

weixin_58670090的博客 6270

Cisco Packet Tracer 7.3免登录技巧:3种方法快速跳过注册界面(附详细截图)

本文针对Cisco Packet Tracer 7.3启动时遇到的登录界面问题,提供了三种合法高效的解决方案。详细介绍了如何利用官方访客模式快速启动、注册思科账号实现一劳永逸登录,以及通过调整本地安装配置优化启动流程,帮助网络学习者与备考者绕过繁琐步骤,快速进入实验环境。

weixin_29305337的博客 764

杭电oj ——1000 A+B问题

从今天开始推出新专栏——杭电oj 题库啦。首先附上杭电oj的链接 acm.hdu.edu.cn 我们再来说一下杭电的注册,和刷题的界面。 下图为杭电的初始界面,首先我们需要创建账户才能使用,杭电界面本为全英文,这里借用了翻译器进行翻译。本人认为,如果不是需要专门的打acm训练赛的话,用翻译版的体验感比较好,在题目中有很多的专有名词需要查字典,只是单纯地刷题,提高代码能力,使用翻译更好。 准备做好后,我们就开始刷题了。刷题界面每页有100道题。 这里题目不完全按照杭电的顺序来...

qq_68162134的博客 3142

2023华为od机试真题【恢复数字序列】C++

2023华为od机试真题【恢复数字序列】C++ 对于一个连续正整数组成的序列,可以将其拼接成一个字符串,再将字符串里的部分字符打乱顺序。如序列8 9 10 11 12,拼接成的字符串为89101112,打乱一部分字符后得到90811211,原来的正整数10就被拆成了0和1。现给定一个按如上规则得到的打乱字符的字符串,请将其还原成连续正整数序列,并输出序列中最小的数字. 输入描述

MISAYAONE的博客 5743

HDU 题目分类

 1001       这个就不用说了吧  1002       简单的大数  1003       DP经典问题,最大连续子段和  1004       简单题  1005       找规律(循环点)  1006       感觉有点BT的题,我到现在还没过  1007       经典问题,最近点对问题,用分治  1008       简单题  1

简放视野的专栏 1635

模拟一个基站和一个移动台之间的信号传播

同样使用时间序列和上述参数计算出反射径信号的信号强度。根据时间序列和上述参数,计算出直射径信号的信号强度。该信号被视为一个简单的正弦波信号,其频率是2Hz。:时间序列,从0.1秒开始,每隔0.0001秒采样一次,直到12秒。:设置坐标轴范围,横轴范围为0到12秒,纵轴范围为-0.5到0.5。:移动台移动的速度。:移动台距离基站的初始距离。:基站到反射墙的距离。假设基站到反射墙的距离为15米。:绘制直射径信号、反射径信号和合成信号的波形图。:添加标签,标注每条曲线对应的含义。表示反射径信号的颜色为绿色,

Fanjufei的博客 296

【ARM CoreLink 系列 4.2 -- NI-700 Function units 详细介绍】

CoreLink NI-700的ASNI(AXI系统网络接口)完成者单元负责接收并处理来自AXI请求者设备的请求。这些单元将事务打包成根据NI-700通用传输(GT)协议的Flits,并将GT响应Flits解包成AXI响应。AXI与GT协议之间的请求、数据和响应事务的转换:确保AXI事务可以被NI-700网络正确处理,并将GT协议的响应正确转换回AXI响应。入站事务的突发拆分。

CodingCos的博客 1331

杭电OJ - 1022 Train Problem I(栈应用)

HDOJ-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...

TimeDeath的博客 299

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【杭电oj】1022 - Train Problem I(栈)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28971    Accepted Submission(s): 10987 Problem Description As the n

wyg1997 935

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

杭电OJ【1022】Train Problem I 栈

解题思路: 进栈出栈问题。 代码: #include #include #include using namespace std; int main() {     int n,io[100];     char in[100],out[100];     while(~scanf("%d %s %s",&n,in,out))     {         stac

戦う! 276

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

杭电ACM OJ 1022 Train Problem I 回溯法求出栈遍历序列 很强大

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 40163    Accepted Submission(s): 15058 Problem Description As the n

qq_36523667的博客 628

杭电acm1022 Train Problem I

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 40640    Accepted Submission(s): 15217 Problem Description As the n

cwhong_723的博客 781

杭电hduoj 1022 Train Problem I

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 算法特工队QQ群:979618872 (伸手党绕边,欢迎有良好基础的人加入) // // Created by Leo Lee on 2019/4/4. // #include <iostream> #include <algorithm> #include <...

tobe_numberone的博客 288

HDU 1022 Train Problem I【栈】

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1022 需要注意的是:出和入不一定全出,全入 #include #include using namespace std; int main() { int i,n,j,k,way[100]; //way[i]=0表示出栈,=1为进 stacks; char str1[100],str2[100

某科学的程序员 536

杭电OJ 1022:Train Problem I

这个题目主要就是模拟出栈和入栈,其实不难就是有点麻烦。话不多说,直接上代码: #include #include using namespace std; int main(){ int n;int in[10],out[10];stack S; int result[20];char t1[10],t2[10]; while(scanf("%d\n",&n)!=EOF){ whi

533

1022 Train Problem I 栈的简单应用

题意栈的简单应用,给出元素个数,入栈顺序O1和出栈顺序O2,每个序列里有n个元素,问能不能利用栈把O1变成O2。 版本1:依此考虑出栈的元素(记为temp),首先判断为了temp出栈,是否需要有元素入栈,一种是需要,则入栈,入栈之后的栈顶元素则为temp;若不需要入栈,那么因为栈的特点,在正常情况下,栈顶元素为temp,若不是,则O1不能转换成O2,此时则退出循环。而对于栈顶元素为temp的情况,则

lizongkai-big 339

杭电acm—1022 Train Problem I

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46308    Accepted Submiss...

HGS的博客 669

杭电1022 Train Problem I

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25879    Accepted Submission(s): 9771 Problem Description As the new

GoodLuckAC的博客 556

Java 杭电ACM Train Problem I 1022

点击打开链接 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 Trai

yangxin_blog的专栏 648

Hdu 1022 Train Problem I 程序参考

http://acm.hdu.edu.cn/showproblem.php?pid=1022 #pragma warning(disable : 4786) #include &lt;iostream&gt; #include &lt;stack&gt; #include &lt;string&gt; #include &lt;vector&gt; using na...

数据库天地 134

HDUOJ-------(1022)Train Problem I

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16859    Accepted Submission(s): 6300 Problem Description As the new term c...

weixin_33881140的博客 110
上一篇: 【杭电-oj】-2089-不要62(打表)
下一篇: 【Poj】-1363-Rails(栈)
Bear1997
博客等级 码龄11年 55粉丝 369原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值