Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24980 Accepted Submission(s):
9424
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
此题并不像题中测试数据一样好像是逆序输出即可,而是要用栈的思维,并不是说必须所有的车都进站了 才可以统一按顺序出来,而是说可以先进入一辆车然后这辆车出来,然后再进下一辆车,也可以先进两辆然后出来一辆再进一辆,即何时进站何时出站都可以(前提是符合题目的进出站要求即先进后出)
STL代码:
#include<stdio.h> #include<string.h> #include<stack> #define MAX 1100 using namespace std; int main() { int n,m,j,i,t,k; char jin[MAX],chu[MAX];//进展顺序和出站顺序数组 int vis[MAX];//记录第i次是进站还是出站 while(scanf("%d",&t)!=EOF) { scanf("%s %s",jin,chu); stack<char>s;//定义一个字符型的栈 memset(vis,0,sizeof(vis)); for(i=0,j=0,k=0;i<t;i++) { s.push(jin[i]); vis[k++]=1; while(!s.empty()&&s.top()==chu[j])//如果站不为空且栈顶的元素与出站顺序的元素相同 { j++;//删除已出的元素 vis[k++]=0;//出站并标记 s.pop();//删除栈首元素 } } if(s.empty()) { printf("Yes.\n"); for(i=0;i<k;i++) { if(vis[i]) printf("in\n"); else printf("out\n"); } } else printf("No.\n"); printf("FINISH\n"); } return 0; }
用数组实现:
#include<stdio.h> #include<string.h> #define MAX 1100 char zhan[MAX]; char jin[MAX],chu[MAX];//进展顺序和出站顺序数组 int vis[MAX];//记录第i次是进站还是出站 int main() { int n,m,j,i,s,sum,t,top,k; while(scanf("%d",&t)!=EOF) { scanf("%s %s",jin,chu); memset(vis,0,sizeof(vis)); top=0;k=0; for(i=0,j=0,k=0;i<t;i++) { zhan[top++]=jin[i];//存栈 vis[j++]=1; while(top>=0&&zhan[top-1]==chu[k])//如果站不为空且栈顶的元素与出站顺序的元素相同 { vis[j++]=0;//出站并标记 top--;//删除栈首元素 k++;//删除已出的元素 } } if(top<0) { printf("Yes.\n"); for(i=0;i<j-1;i++) { if(vis[i]) printf("in\n"); else printf("out\n"); } } else printf("No.\n"); printf("FINISH\n"); } return 0; }