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 ...
随机推荐
- python 三——列表、字典、元祖、字符串、set
本节内容 1.列表 2.元祖 3.字典 4.字符串 不可变类型:整型.字符串.元组tuple 可变类型:列表list.字典dict 1.列表 >>> names ['Alex', ' ...
- Java-将字符串转为数字
package com.tj; public class MyClass implements Cloneable { public static void main(String[] args) { ...
- Java-对复合类型数据进行排序
Array.sort(arr)可以进行简单的排序,如果需要复杂的排序可以实现Comparable package com.tj; import java.util.Arrays; public cla ...
- Linux快捷键列表
Linux快捷键列表 快捷键 功能描述 快捷键 功能描述 control+p 查询命令历史纪录的上一条命令 方向上键 查询命令历史纪录的上一条命令 control+n 查询命令历史纪录的下一条命令 方 ...
- ubuntu14.04 Cannot find OpenSSL's <evp.h>
Cannot find OpenSSL's <evp.h> when i configure php7 manually,i get trouble with that problem,f ...
- Laya List翻页滚动方案 & List滚动源码解析
Laya List翻页滚动方案 & List滚动源码解析 @author ixenos 2019-03-29 1.List翻页滚动方案 /** * 计算下一页的起始索引, 不足时补足 * @p ...
- 2017 Multi-University Training Contest - Team 2
Regular polygon Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- WebLoad XML-parser methods
WebLOAD provides an embedded, third-party XML parser object to improve the multi-platform support fo ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- P2014 选课 (树形动规)
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...