Question

Sutmissien shoud indude a cop file and an output file generated by your program. Plee smit the pp fie Do NOT submit the entir
from track A an incoming order 1,2,3,4, 5 to track B with an outgoing order 5, 3, 2, 1, 4 using the station, which can be tre
For instance,if the test case is 1, 2, 3, 4, 5, then we can perform the following sequence of Track A Stack (left side is t
Sutmissien shoud indude a cop file and an output file generated by your program. Plee smit the pp fie Do NOT submit the entire Visual Studio project package! Background hre s amous ralway staton in PopPush City. Country there is incredibly hilly. The station was built in st centiuny Unfortunately, funds were extremely limited that time. it was possible to establish only a sete res Moreover t turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track 1, 2 3, 4, 5 8432 B. Station The tradition is hat every train arriving from the direction A continues in the direction B with coaches reorgani i seme way Assume tha: the train anv from the direction A has N s 1000 coaches numbered in increasing order 1,2, N The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a, an..a. Help him and write a program that decides whecher 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 an d that they can move themselves until they are on the track in the direction B . 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 in the direction A and also once it has left the station in the direction B it cannot return back to the station Program 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, and 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 track A an incoming order 1,2,3,4, 5 to track B with an outgoing order 5, 3, 2, 1, 4 using the station, which can be treated as a stack The last ine of the block contains just O Iif a block starts with a zero, the program weill terminate. e You shouild use the input file named lab1in.txt (download from Canvas) to test your program; an output file named lab1out.txt(with correct output) is also provided for you to verify your program. Input Sample I/ start of first block 2345 s4123 1/ end of first block // start of second block 5432 /I end of second block // end of Input Program 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 ine of the input file. Otherwe it contains "No". In addinion, there is one empty line after the lines espanding to one b'ok uf the inpus file. There is no line in the output file corresponding to the last back of the input t Output Sample This is an output sample of the previous input sample Hint Key points to understand/solve the problem * The train station can be regarded as a stack . One can push a coach from track A into the stack when the coach is popped out of the ack g into track B, and a coach in the station can never go back to track A * Taking a train with 5 coaches as an example Coaches in track A is always in strictly increasing order, Le·1,2,3,4,5 * The train chief tries to marshal the coaches anto track ธ ฟ, a seomee-Pah ~ ~- * Each test case in the input fle is asking a que hon such that the coach order matches the test case? * 11,toma h.Pe~a.노m-
For instance,if the test case is "1, 2, 3, 4, 5", then we can perform the following sequence of Track A Stack (left side is the top) Track B operation 2, 3,4,5 empty push(1) 2,3,4, 5 empty pop 3,4,5 3,4,5 empty 1,2 pop 4, 5 1,2 4, 5 2. 3 empty 1,2,3 1,2,3,4 empty pop empty 1,2,34 push(5) 1, 2, 3, 4, 5 empty The final coach order in track B matches 1,2,3,4,5- in the test case, so program outputs YES o Now use a similar way to validate that why test case "5, 4, 1,2,3" will NOT work Now you should be able to handle larger test cases as the ones in the input file provided by the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

#include<cstdio>
#include<stack>

using namespace std;
//global variables
int N, c;
stack<int> block;

void railway() {
//infinit for loop
for(;;) {
//check for size of block
while(block.size() > 0) block.pop();
int j = 0;
for(int i = 0; i < N; i++) {
scanf("%d", &c);
if(c == 0) return;

while(j < N && j != c) {
if(block.size() > 0 && block.top() == c) break;
j++;
block.push(j);
}
if(block.top() == c) block.pop();
}
if(block.size() == 0) printf("Yes\n");
else printf("No\n");
}
}

int main() {
//infinite loop if user press 0 than break
for(;;) {
//store the value of N
scanf("%d", &N);
//if N is 0 than break
if(N == 0) break;
//call the function to check
railway();
printf("\n");
}
}

Screenshots -

Add a comment
Know the answer?
Add Answer to:
Sutmissien shoud indude a cop file and an output file generated by your program. Plee smit the pp fie Do NOT subm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A...

    Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A user event may be “pressing the Enter key on keyboard” or “clicking a mouse button”. Stack is a data structure with the Last-In-First-Out property (LIFO). If we push the aforesaid user events into a stack, a computer program using that data structure can “rewind” user events by popping them out of stack one at a time. This backtracking feature is available as Edit ->...

  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • 12.8 GPA reports using files This program is to compute and write to a file the...

    12.8 GPA reports using files This program is to compute and write to a file the GPA for student scores read from an input file. As in Lab 7.5, the point values of the grades are 4 for A, 3 for B, 2 for C, 1 for D, and 0 for F. Except for the grade A, where + has no effect, a + after the letter increases the point value by 0.3, and except for F, where a -...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • For this project, you are tasked with creating a text-based, basic String processing program that performs...

    For this project, you are tasked with creating a text-based, basic String processing program that performs basic search on a block of text inputted into your program from an external .txt file. After greeting your end user for a program description, your program should prompt the end user to enter a .txt input filename from which to read the block of text to analyze. Then, prompt the end user for a search String. Next, prompt the end user for the...

  • I'm having trouble writing this code, can some help me? Step 1: Capturing the input The...

    I'm having trouble writing this code, can some help me? Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT