Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Write in code in C language Exercise 2.19 .(*****) Two Stacks This problem asks you to determine a stack-generated permutation in a more general

image text in transcribedPlease Write in code in C language

Exercise 2.19 .(*****) Two Stacks This problem asks you to determine a stack-generated permutation in a more general setting. A permutation of 1 to n is a stack-generated permutation if and only if it can be generated by pushing 1 to n onto a stack and popping them off. For example, stack-generated permutation (2,1,3) can be generated by doing operations push 1, push 2, pop, pop, push 3, pop. In this problem, instead of using only one stack, we use two stacks: every time you can push an element to either stack, or you can pop element from either stack as long as it is non-empty. However, once an element is popped, it cannot be pushed back to either stack again. Your task is to determine whether a permutation can be generated by using two stacks. Input There are several test cases in the input file. The fist line is an integer t20, which indicates the number of test cases. For each test case, the first line contains an integer 1n1000. The following line contains n integers separated by a space, which is a permutation of 1 to n. Output For each test case, output one line: "Yes" if the permutation can be generated by using two stacks; "No", otherwise. Sample Input Sample Output Yes Yes No No Explanation We consider the first test case. It can be generated by the following operation sequence: push 1 to stack 1, pop stack 1, push 2 to stack 1, push 3 to stack 1, pop stack 1, push 4 to stack 2, push 5 to stack 1, pop stack 1, push 6 to stack 2, push 7 to stack 2, pop stack 2, pop stack 1, pop stack 2, pop stack 2 . 55

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

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago