D. Train problem I

Source: Internet
Author: User
Tags exit in
D. Train problem I Time limit:
1000 ms Case time limit:
1000 ms Memory limit:
32768kb64-bit integer Io format:
% I64dJava 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 shoshould output a line contains" yes. ", and then output your way in exchanging the order (you shocould output" in"
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.".


Solution: This is a simulated question. knowledge points are stack and queue applications, which can be simulated using arrays.

A maximum of 9 trains, numbered 1, 2, 3... 9 are displayed ). Give an N value, indicating the number of trains to be taken in and out of the station. Give the train arrival sequence table and the train exit sequence table, ask if the train in the inbound/outbound table can exit in the order of the outbound table. If yes, output "yes. "And the inbound and outbound information (In, out); otherwise, the output is" No. ", the last line output by each group of test cases ends with" finish.

It is simulated with three queues and one station, storing them separately, the train arrival order, the train exit order, and the station's train situation (the entry and exit order requirements ). Enter the train in and out sequence table, use the character array for intermediary storage, and save it to the corresponding queue. According to the question, the maximum number of trains entering and leaving the station is 2 * n (each train enters the station once, and the arrival time). At the beginning, the judgment conditions are entangled for a long time. Because a train can contain up to 9 vehicles, this location does not cause program running timeout. In each loop, it is determined that if there is a train in the station that can exit the station in the given order, let it exit (use the queue to store the train's inbound and outbound conditions). Otherwise, if a train does not enter the stack, let the train enter the station. At last, if there are 2 * n pieces of data in the train's inbound/outbound storage Queue (all trains are in and out, in the order given by the question), the output is "yes. "and train entry and exit conditions and sequence (direct output of the entry and exit storage Queue); otherwise, output" no. ", and the rest can be output according to the meaning of the question.


# Include <stdio. h> # include <string. h> // use string # include <iostream> // use C ++ output stream # include <stack> // use stack # include <queue> // Use Queue using namespace STD; int main () {int n, x, I; string st; // temporary while (scanf ("% d", & N) during output )! = EOF) {queue <int> In, out; // The storage requires inbound traffic. The train number of the outbound traffic is queue <string> sign; // stores the status (in, out, and ordered) stack of the train storage station <int> S; // stores the order of incoming and outgoing trains in the station char a [20]; // during storage input, the train enters and exits, and the backend will be transferred to the scanf Queue ("% s", ); // read the train entry sequence for (I = 0; I <n; I ++) in. push (A [I]-48); // transfers the train inbound order to the inbound queue scanf ("% s", a); for (I = 0; I <n; I ++) out. push (A [I]-48); X = in. front (); // read the first bus id s. push (x); // put in stack (station) in. pop (); sign. push ("in"); // record the arrival of a train int m = 2 * n-1; // process a maximum of 2 * n times (n times, N times out) while (M --) {If (! Out. Empty ()&&! S. empty () & out. front () = S. top () // if there is a train in the station (stack), there is a train serial number in the outbound queue, and the train to be processed is the same train, the train will be processed (for combat) {out. pop (); // This train can exit S. pop (); // The train exit sign. push ("out"); // record the train exit} else if (! In. empty () // if a train at the exit cannot exit and the train does not enter the station, let the next train enter the station {x = in. front (); // extract incoming train information. pop (); // The train will enter S. push (x); // train inbound sign. push ("in"); // record 1 train inbound} If (sign. size () = N * 2) // if the number of incoming and outgoing trains is 2 * n (each train enters the station once and leaves the station once) {printf ("yes. \ n "); // The train enters the station in the given order and can exit in the given order for (I = 0; I <2 * n; I ++) {ST = sign. front (); cout <st <Endl; sign. pop () ;}} else printf ("no. \ n "); // The train enters the station in the given order, and cannot exit printf (" Finish \ n ") in the given order;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.