HDU 1022 Train Problem I (数据结构 —— 栈)
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.



More details in the Sample Input.
for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
3 123 321
3 123 312
Yes.
in
in
in
out
out
out
FINISH
No.
FINISHHintHint
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.".
题意:给出初始序列和序列a和b。和一个栈c 。a仅仅能进c。不能回来。c仅仅能进b,求是否能达到目标并打印路径。
典型的栈,直接模拟。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int MAX=0x3f3f3f3f;
int n ,op[4004];
char a[2005], b[2005], c[2005];
int main()
{
while(~scanf("%d %s %s",&n,a,b)) {
int i=0,j=0,top=-1,k=0;
while(j<n) { // b里还有车没匹配
if( a[i] == b[j] ) { //a的第一辆与b直接匹配
op[k++] = 1;
op[k++] = 0;
i++,j++;
} else if( top >= 0 && c[top] == b[j]) { // 栈里的车与b匹配
op[k++] = 0;
j++,top--;
} else if(i<n) { // 都不匹配就把a里的车入栈
top++;
c[top] = a[i];
op[k++] = 1;
i++;
} else break; //a里没车了直接退出循环
}
if(j >= n) {
printf("Yes.\n");
for(int i=0;i<k;i++)
if(op[i] == 1) printf("in\n");
else printf("out\n");
} 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【栈的应用】【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(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- HDOJ/HDU 1022 Train Problem I(模拟栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu 1022 Train Problem I(栈)
#include<iostream> #include<vector> #include<cstring> #include<string> #incl ...
- HDU 1022 Train Problem I 用栈瞎搞
题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...
- 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【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- POJ-2002 Squares,哈希模板+数学公式!
Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...
- [BZOJ3378] [Usaco2004 Open]MooFest 狂欢节(树状数组)
传送门 开2个树状数组 一个存的是下标,一个存的是数量 细节...看标称吧,懒得说了,好气啊 #include <cstdio> #include <iostream> #in ...
- 2013 Dhaka 区域赛
A.uva 12709 Falling ANTS 首先按照H排序,然后按照L*H*W排序 #include<iostream> #include<cstdio> #includ ...
- ngrinder的安装
1.官网下载war包(ngrinder-controller),可以使用tomcat启动或者直接nohup java -XX:Permsize=200m -jar ngrinder-3.4.1.war ...
- EC++学习笔记(三) 资源管理
条款13:以对象管理资源 资源包括:动态分配内存.文件描述符.网络socket.互斥锁.数据库连接 把资源放进对象内,可以依赖c++的"析构函数自动调用机制"确保资源被释放智能指针 ...
- ftrace 提供的工具函数
内核头文件 include/linux/kernel.h 中描述了 ftrace 提供的工具函数的原型,这些函数包括 trace_printk.tracing_on/tracing_off 等.本文通 ...
- H5 折线图插件
一.可以使用Highcharts,参考网址:https://api.hcharts.cn/highcharts: 二.可以使用Echarts,参考网址:http://echarts.baidu.com ...
- 不拖控件的asp.net编程方法——第1回
以前写的asp.net程序基本上都用了webfrom的控件编写的,当然有个好处就是易入门.快速效率高,但感觉自己这了几个小系统,还是没学到什么东西,感觉心里没底,因为都是封装好的东西,拿来就用的,功能 ...
- LCD1602和LCD12864
1602 字符型(字符型的模组) 只能显示数字与字符,不可以显示汉字 12864 点阵型(图形型的模组) 可根据需求任意显示字符 数字 汉字 图形 1602 为16字乘2行12864 为128列点阵乘 ...
- HUD 1506 Largest Rectangle in a Histogram
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...