D. Train Problem ITime Limit:
1000msCase Time Limit:
1000msMemory Limit:
32768KB64-bit integer IO format:
%I64d Java class name:
Main Submit
Status
PID: 5246
Font Size: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.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.".
解題思路:該題為類比題,知識點為棧和隊列的應用,可以用數組類比。
最多9輛火車,編號為1,2,3....9,進出站方式入圖(先進後出)。給出一個n,表示要進出站得火車數,給出火車進站順序表,給出火車出站順序表,問按給出入站表入站的火車能否以出站表順序出站,如果可以,輸出“YES.”以及出入情況資訊(in,out),否則輸出“NO.”,每組測試案例輸出的最後一行以“FINISH”結束。
用3個隊列及1個站來類比,分別儲存,火車進站順序,火車出站順序,站內火車情況(出入順序要求情況)。輸入火車進、出站順序表,用字元數組做中介儲存,後存入到對應隊列。依題意得,火車進出站的次數最多為2*n次(每輛火車進站一次,出站一次),開始就迴圈結束判斷條件糾結了很久,後面才想到。因為火車最多9輛,所以,這個地方不會導致程式運行逾時。每次迴圈,判斷出來,如果站內有火車可以按照給定的順序出站,則讓其出站(用隊列儲存火車出入情況),否則,若有火車未入棧,則讓火車入站。最後如果火車進出站情況儲存隊列中有2*n個資料(所有的火車都進過站並且出來了,按題目給定的順序)則輸出“YES.”和火車出入情況及順序(直接輸出出入情況儲存隊列),否則輸出“NO.”,其餘按題意輸出即可。
#include<stdio.h>#include<string.h> //使用String串#include<iostream> //使用c++輸出資料流#include<stack> //使用棧#include<queue> //使用隊列using namespace std;int main(){ int n,x,i; string st; //輸出時內容暫存 while(scanf("%d",&n)!=EOF) { queue<int> in,out; //儲存需要入站,出站的火車編號 queue<string> sign; //儲存火車儲存站的狀態(in,out,有序) stack<int > s; //儲存現在車站中火車進出順序 char a[20]; //儲存輸入時,火車進、出站順序,後面會轉存到隊列 scanf("%s",a); //讀入火車入站順序 for(i=0;i<n;i++)in.push(a[i]-48); //將火車入站順序轉存到入站隊列 scanf("%s",a); for(i=0;i<n;i++)out.push(a[i]-48); x=in.front(); //讀取第一輛進站火車的車號 s.push(x); //放入棧(站中) in.pop(); sign.push("in"); //記錄有一輛火車進站 int m=2*n-1; //最多處理2*n次(進n次,出n次) while(m--) { if(!out.empty()&&!s.empty()&&out.front()==s.top()) //若站(棧)內有火車,出站隊列中有火車序號,且要處理的為同一輛火車,則處理這輛火車(出戰) { out.pop(); //該火車可以出站 s.pop(); //該火車出站 sign.push("out"); //記錄有火車出站 } else if(!in.empty()) //若處於出站口的火車不能出站,且還有火車沒有進站,則讓下一輛火車進站 { x=in.front(); //提取即將進站火車資訊 in.pop(); //該火車將入站 s.push(x); //火車入站 sign.push("in"); //記錄有1輛火車入站 } } if(sign.size()==n*2) //若火車出入站的次數為2*n次(每輛火車進一次站,出一次站) { printf("Yes.\n"); //火車以給出順序進站,能以給出順序出站 for(i=0;i<2*n;i++) { st=sign.front(); cout<<st<<endl; sign.pop(); } } else printf("No.\n"); //火車以給出順序進站,不能以給出順序出站 printf("FINISH\n"); } return 0;}