您的位置:首页 > 大数据 > 人工智能

HDU 1022 Train Problem I && PAT 5-2 列车厢调度 栈

2017-03-12 22:02 579 查看

Train Problem I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 34997 Accepted Submission(s): 13184

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022

题解:

确实是很直白的一个栈,不过处理起来觉得还是有点绕。将输入和输出序列分别读入in[maxn]和out[maxn],
当栈为空或者当前栈顶元素和应出栈元素不符的时候,要入栈(入栈队列还有元素的话);
当栈顶元素和应出栈的元素相同时出栈;其他情况则均为No.
用flag数组来标记进出栈的情况。


#include <bits/stdc++.h>
#define INF 0xffffffff
using namespace std;
typedef long long ll;

const int maxn = 50;

char in[maxn];
char out[maxn];
int flag[maxn];

int main(){
int n;
while(scanf("%d", &n) == 1 && n){
scanf("%s%s", in, out);
stack<char> s;
int i=0, j=0;
int cnt = 0;
while(j < n){
if(s.empty() || s.top() != out[j] && i<n){
s.push(in[i++]);
flag[cnt++] = 1;
}
else{
if(s.top() == out[j]){
s.pop();
flag[cnt++] = 0;
j++;
}
else{
break;
}
}
}
if(j == n){
printf("Yes.\n");
for(int i=0; i<cnt; i++){
if(flag[i]){
printf("in\n");
}
else{
printf("out\n");
}
}
}
else{
printf("No.\n");
}
printf("FINISH\n");
}
return 0;
}


5-2 列车厢调度 (25分)

1  ======   <--移动方向
/
3 =====
\
2  ======   -->移动方向


大家或许在某些数据结构教材上见到过“列车厢调度问题”(当然没见过也不要紧)。今天,我们就来实际操作一下列车厢的调度。对照上方的ASCII字符图,问题描述如下:

有三条平行的列车轨道(1、2、3)以及1-3和2-3两段连接轨道。现有一列车厢停在1号轨道上,请利用两条连接轨道以及3号轨道,将车厢按照要求的顺序转移到2号轨道。规则是:

每次转移1节车厢;

处在1号轨道的车厢要么经过1-3连接道进入3号轨道(该操作记为”1->3”),要么经过两条连接轨道直接进入2号轨道(该操作记为”1->2”);

一旦车厢进入2号轨道,就不可以再移出该轨道;

处在3号轨道的车厢,只能经过2-3连接道进入2号轨道(该操作记为”3->2”);

显然,任何车厢不能穿过、跨越或绕过其它车厢进行移动。

对于给定的1号停车顺序,如果经过调度能够实现2号轨道要求的顺序,则给出操作序列;如果不能,就反问用户 Are(你) you(是) kidding(凯丁) me(么)?

输入格式:

两行由大写字母组成的非空字符串,第一行表示停在1号轨道上的车厢从左到右的顺序,第二行表示要求车厢停到2号轨道的进道顺序(输入样例1中第二行CBA表示车厢在2号轨道的停放从左到右是ABC,因为C最先进入,所以在最右边)。两行字符串长度相同且不超过26(因为只有26个大写字母),每个字母表示一节车厢。题目保证同一行内的字母不重复且两行的字母集相同。

输出格式:

如果能够成功调度,给出最短的操作序列,每个操作占一行。所谓“最短”,即如果1->2可以完成的调度,就不要通过1->3和3->2来实现。如果不能调度,输出 “Are you kidding me?”

输入样例1:

4000

ABC

CBA

输出样例1:

1->3

1->3

1->2

3->2

3->2

输入样例2:

ABC

CAB

输出样例2:

Are you kidding me?

题解:

和上一题一样,也是栈,代码只需要增加刚入栈就出栈的时候是输出“1->2”就可以了。


#include <bits/stdc++.h>
#define INF 0xffffffff
using namespace std;
typedef long long ll;

const int maxn = 50;

char in[maxn];
char out[maxn];
int flag[maxn];

int main(){
while(scanf("%s%s", in, out) == 2){
stack<char> s;
int i=0, j=0;
int cnt = 0;
int leni = strlen(in);
int leno = strlen(out);
while(j < leno){
if(s.empty() || s.top() != out[j] && i<leni){
s.push(in[i++]);
if(s.top() == out[j]){
s.pop();
j++;
flag[cnt++] = 2;
}
else{
flag[cnt++] = 0;
}
}
else{
if(s.top() == out[j]){
s.pop();
j++;
flag[cnt++] = 1;
}
else{
break;
}
}
}
if(j == leno){
for(int i=0; i<cnt; i++){
if(flag[i] == 0){
printf("1->3\n");
}
else if(flag[i] == 1){
printf("3->2\n");
}
else{
printf("1->2\n");
}
}
}
else{
printf("Are you kidding me?\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: