Hdu 1022 Train Problem I 栈
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51413 Accepted Submission(s): 19341
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
HintHint
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.".
题解:题目要求可转化为能否将s1元素进栈,然后输出为s2的排列形式,所以我们在使s1元素进栈的时候和s2的元素匹配,并记录顺序,若最后s1的全部输出完,那么就成立;
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
char s[55],ss[55];
bool in[11111];
int main()
{
int n;
while(~scanf("%d %s%s",&n,s,ss)){
stack<char>p;
int j=0,k=0;
for(int i=0;s[i];i++){
p.push(s[i]);
in[k++]=1;
while(!p.empty()&&p.top()==ss[j]){
//将可以与ss匹配的出栈,加入与ss一样的队形
in[k++]=0;//记录步骤
p.pop();
j++;
}
}
if(p.empty()){
printf("Yes.\n");
for(int i=0;i<k;i++)
printf("%s\n",in[i]?"in":"out");
}else{
printf("No.\n" );
}
printf("FINISH\n");
}
return 0;
}
Hdu 1022 Train Problem I 栈的更多相关文章
- HDU 1022 Train Problem I(栈的操作规则)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1022 Train Problem I【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022.Train Problem I【栈的应用】【8月19】
Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...
- HDU - 1022 Train Problem I STL 压栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]
Train Problem I 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...
- HDU 1022 Train Problem I
A - Train Problem I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1022 Train Problem
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- 其它综合-有趣的linux命令行工具-lolcat
lolcat :一个在 Linux 终端中输出彩虹特效的命令行工具 何为Lolcat Lolcat 是一个针对 Linux,BSD 和 OSX 平台的工具,它类似于 cat,并为 cat 的输出添加彩 ...
- 清明培训 清北学堂 DAY2
今天是钟皓曦老师的讲授~~ 总结了一下今天的内容: 数论!!! 1.整除性 2.质数 定义: 性质: 3.整数分解定理——算数基本定理 证明: 存在性: 设N是最小不满足唯一分解定理的整数 (1) ...
- 洛谷P3953逛公园
题目 作为\(NOIp2017D1T3\) 这个题还是很良心的,至少相对于\(NOIp2018\)来说,希望\(NOIp2019\)不会这么坑吧. 这个题可以作为记忆化搜索的进阶题了,做这个题的方法也 ...
- JDK源码分析(7)String
String String表示字符串,Java中所有字符串的字面值都是String类的实例,例如"ABC".字符串是常量,在定义后不能被改变,字符串缓冲区支持可变的字符串.因为St ...
- U66785 行列式求值
二更:把更多的行列式有关内容加了进来(%%%%%Jelly Goat奆佬) 题目描述 给你一个N(n≤10n\leq 10n≤10)阶行列式,请计算出它的值 输入输出格式 输入格式: 第一行有一个整数 ...
- python中的__metaclass__
什么是元类: python中类也是一种对象, 可以称为类对象. 元类就是用来创建类对象的"东西". 你创建类就是为了创建类的实例对象, 不是吗? 但是我们已经学习了python中的 ...
- Docker:Docker 性质及版本选择 [三]
一.Docker的性质 Docker的组成其实很简单.你需要搭建registry,专属于你自己的私有仓库,然后就是docker的镜像和docker的容器.Docker的镜像,就类似与windos的系统 ...
- IE提示“Internet Explorer已限制此网页运行脚本或ActiveX控件”的解决办法
在页面html开始标签和head开始标签中间新增一行,添加以下代码: <!-- saved from url=(0014)about:internet --> 或者 直接设置IE浏览器 工 ...
- 2018-2019-2 《Java程序设计》第3周学习总结
20175319 2018-2019-2 <Java程序设计>第3周学习总结 教材学习内容总结 第三周通过课本与蓝墨云上的视频学习了<Java2实用教程>第四章类与对象 成功激 ...
- windows下 cmd 界面的替代者 cmder 推荐!
介绍 http://cmder.net/ Portable console emulator for Windows Cmder is a software package created out o ...