Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Railroad cars numbered 1 , 2 , . . . , n on the right track are to be permuted and moved along on the

Railroad cars numbered 1,2,..., n on the right track are to be permuted and moved along on the left track. A car may be moved directly onto the left track, or it may be shunted onto the siding to be removed at a later time and placed on the left track. The siding thus operates as a stack, a push operation moving a car from the right track onto the siding and a pop operation moving the "top" car from the siding onto the left track. For example, when n =3, push 1, push 2, move 3, pop 2, pop 1 arranges them in the order 3,2,1.
Are any permutations not possible? Yes, for example, 312 is an impossible permutation.
Assume that the train arriving from the direction A has N <=1000 coaches numbered in increasing order 1,2,3,....N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the same direction so that their order will be. Your job is to help him to write a program that decides whether it is possible to get the required order of coaches.
You can assume that single coaches can be disconnected from the train before they enter the station and that they can move until they are on the track in the same direction. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track on the right and also once it has left the station in the direction on the left it cannot return back to the station.
input
The input file consists of blocks of lines, each of which is a test case. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block, there is the integer N, which is the number of coaches in the train. In each of the next lines of the block there is a permutation of 1,2,..., N . For example, if N is 5, the permutation could be 5,3,2,1,4. Your program will take this permutation as input and determine whether you can marshal the coaches from the right track an incoming order 1,2,3,4,5 to the left track with an outgoing order 5,3,2,1,4 using the station, which can be treated as a stack.
The last line of the block contains just 0.
If a block starts with a zero, the program will terminate
output
The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise, it contains No. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last null'' block of the input file.
Sample Input:
5 start of the first block
12345
54123
0 end of the first block
6 start of the second block
654321
0 end of the second block
0 end of input
Sample Output:
Yes
No
Yes
input file: A7in.txt
162
101391384281176331487411491185212815313047179511018
702411226641151413087846348036133839102617612023156
10611668851475912178375350549110160942210771123667
1031451527513473108251407710412720812154961518613588
89905819316129359756798212419714221143100129109146
111598554513343161489211342658312512611451362144157
15057401361371591621311196944991581433914910515324112
15512213284667216027
12659874312111310171820222125313032332934352841
40423943384647485150494552445655545337573658596027
26616224231916656664676863157574737276777980828381
858887949395929697919099102101104105103107111110109
1081061001129889114117116118115113119121120123122124126
127128130129125131132135136139138140137134133142145148
14914715315515415215115615014614416015915815716114314186
847871706914162
144441364012814181151251315855145146428195976482516
202771436311217306534391301176782120851541074215199
96464715916222661253157113389711291810246075413290
122567

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the fruit of the poisonous tree doctrine?

Answered: 1 week ago

Question

What partnerships is your firm currently engaged in?

Answered: 1 week ago