Train Problem I

来源:互联网 发布:如何网络招生 编辑:程序博客网 时间:2024/06/04 18:30

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26062    Accepted Submission(s): 9837


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 3213 123 312
 

Sample Output
Yes.inininoutoutoutFINISHNo.FINISH
C语言程序代码
/*解题思路:: 这个题要用到栈,先定意两个字符数组用来存放进站顺序和出站顺序,边往栈中放进站元素 边判断是否等于出站元素,若不等继续进栈,若相等,则将栈顶元素出栈,然后继续判断。难点::要定义一个整型数组让其存放“0”或“1”,表示“out”或“in”,最后逐个输出。 */#include<stdio.h>#include<string.h>#include<stack>using namespace std;int main(){int n,m,i,j,l,k;char s[10],a[10];int t[101];while(scanf("%d %s%s",&n,s,a)!=EOF){memset(t,0,sizeof(t));stack<char>q;j=k=0;for(i=0;i<n;i++){q.push(s[i]);t[k++]=1;while(!q.empty()&&q.top()==a[j]){t[k++]=0;j++;q.pop();}}if(j==n){printf("Yes.\n");for(i=0;i<k;i++){if(t[i])printf("in\n");elseprintf("out\n");}}elseprintf("No.\n");printf("FINISH\n");}return 0;}
1 0
原创粉丝点击