HUD 1022-Train Problem I
2016-02-02 16:13
573 查看
HUD 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 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.![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/e07a7819adc3fce5ee075d011a0a1d3a.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/3ed5e24625225c0a75f84ac6dbeceabe.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/b72889e34d63c9ba1c9624e8d50888c9.jpg)
输入要求:
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.输出要求:
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.输入样例:
3 123 3213 123 312
输出样例:
Yes.in
in
in
out
out
out
FINISH
No.
FINISH
翻译:
随着新学期的到来,如今Ignatius火车站很繁忙,许多学生想要坐火车回学校(因为Ignatius火车站的火车是全世界最快的)。但是这里有个问题,在所有的火车站点上只有一个铁轨,所以,所有火车都是从一边进入,再从另一边出去。对于这个问题,如果火车A第一个进入铁轨,然后火车B在火车A前离开,一直到火车B离开,火车A都不能离开。这个图片展示了这个问题。现在在火车站上有最多有9个火车,所有的火车有一个编号(从1到n),火车以O1次序进入铁轨,你的任务是确定火车是否能以O2的次序离开。输入包含多个测试数据。每组测试数据是由一个整数(列车的数量)和两个字符串,火车进入的次序是:O1,火车离开的次序:O2,到文件尾结束。
如果你不能交换O1和O2,你要输出一行“No”,否则你应该输出一行“Yes”,然后输出你交换次序的方式(火车入轨为”in”,火车出轨为”out”),最后打印一行“FINISH”。
用c语言写:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct Node{ int num; struct Node *next; }; typedef struct Node *NODE; typedef NODE Stack; Stack CreatStack(); void MakeEmpty(Stack s); void push(int n, Stack s); void pop(Stack s); int top(Stack s); int isEmpty(Stack s); int main(){ int n; int a,b,c; //a指向下一次要进入的火车 //b指向下一次要出去的火车 char in[15] = { 0 }; char out[15] = { 0 }; int step[25]; //数组step和c用来记录火车的进入和出去,默认-1,进入1,出去0 while (scanf("%d %s %s", &n,in,out)!=EOF){ memset(step,-1,sizeof(step)); Stack s = CreatStack(); a = 0; b = 0; c = 0; while (a < n){ //如果栈不是空,或者栈顶不符合当前需要出去的火车吗,就进入 if (isEmpty(s) || top(s) != out[b]-'0'){ push(in[a] - '0', s); step[c++] = 1; a++; } //循环判断可以出去的 while (!isEmpty(s) && top(s) == out[b]-'0'){ pop(s); step[c++]=0; b++; } } if (a == n ){ if (isEmpty(s)){ printf("Yes.\n"); int i = 0; for(i=0;i<c;i++){ if(step[i]==1){ printf("in\n"); } else{ printf("out\n"); } } } else{ printf("No.\n"); } } printf("FINISH\n"); } return 0; } //检查栈是否为空 int isEmpty(Stack s){ return s->next == NULL; } //清空栈 void MakeEmpty(Stack s){ while (!isEmpty(s)){ pop(s); } } //建立栈 Stack CreatStack(){ Stack s; s = malloc(sizeof(struct Node)); s->next = NULL; MakeEmpty(s); return s; } //进栈 void push(int n, Stack s){ NODE new_node; new_node = malloc(sizeof(struct Node)); new_node->num = n; new_node->next = s->next; s->next = new_node; } //出栈 void pop(Stack s){ NODE temp_node = s->next; s->next = temp_node->next; free(temp_node); } //返回栈顶 int top(Stack s){ if (!isEmpty(s)){ return s->next->num; } return 0; }
相关文章推荐
- 真机测试libUMSocial_Sdk_4.2.2.a(UMSShareListController.o)' does not contain bitcode.
- flash air中读取本地文件的三种方法
- 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"
- HDU 1082 Matrix Chain Multiplication
- What is a fully qualified domain name (FQDN)?
- Failed to initialize monitor Thread: Unable to establish loopback connection解决方法
- pair模板类
- hdu5618 Jam's problem again(cdq分治)(BestCoder Round #70)
- MyBaits关联查询一对一、一对多
- 五子棋AI算法第一篇-我对谷歌Alphago的看法
- Lets write a Tiny IoC Container to learn and for fun
- HDU 5618 Jam's problem again (cdq分治+树状数组)
- [原创] zabbix学习之旅四:mail客户端安装
- 【Deep Learning】Review of Stereo Matching by Training a Convolutional Neural Network to Compare Image
- pairing heap 学习以及对于dijkstra算法的再分析
- w2v for sent_pair
- hdu 1021 Fibonacci Again
- UVa 11323 Satisfying Constraints
- go中的main函数和init函数
- visio ppt axure AI svg powerdesign xmind