cs1020e lab 4 (stack and queue)

Post on 06-Jan-2018

231 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Problem 1 Swinging Monkey

TRANSCRIPT

CS1020E Lab 4(Stack and Queue)

Problem 1 : Swinging MonkeyProblem 2 : Alice

SWINGING MONKEYProblem 1

Swinging Monkey• Problem Description:• Monkey can swing from one tree to another directly

as long as there is no tree in between that is taller than or have the same height as either one of the two trees

• Given the sequence of tree heights, determine the number of pair of trees that the Monkey can swing

• Input:• First line is [INTEGER] N: number of trees• Second line is N x [INTEGER]: sequence of height

• Output:• Single line of [INTEGER]: #trees Monkey can swing

Swinging Monkey• Illustration:• 5 trees: 19m – 17m – 20m – 20m – 20m

Output:5

Swinging Monkey• Discussions:• What is the simplest algorithm to solve this?

• Given two trees: check if the Monkey can jump• i.e. Check if there are taller trees in between• Loop for all tree pairs

• Algorithm:• fun canSwing (i, j) { for(k=i+1 to j-1) do{ if(height[k] ≥ min(height[i], height[j]){ return false } } return true}

Swinging Monkey• Observation:• 5 trees: 19m – 17m – 20m – 20m – 17m

Observe:from the tree

before the Blue tree, the

Monkey cannot jump to the

tree after the Blue tree

Swinging Monkey• Idea:• After we process tree i, we can forget all the trees

before i that are shorter than i.• What will be the property of the sequence of trees

that are not forgotten?• Decreasing Order from the tree with smallest index• Why?• Suppose it is not in decreasing sequence• After we process Blue, we can forget

Yellow• Yellow would have been removed

from the sequence

Swinging Monkey• Property of the Sequence:

1. Decreasing Order of Height2. At the start of the processing of the next tree,

the Monkey can jump from all tree in the sequence to the next tree

3. If the next tree is higher than some elements,these elements in the sequence cannot jump beyond the next tree

• Discussions:• What Data Structure do we need to implement?• Stack: why?• Because we need to remember the previous

elements in order

ALICEProblem 2

Alice the Baker• Problem Description:• Given a list of pancakes which can be sweet on

either side or both, given the operations:1. Flip the top X pancakes and add syrup to top2. Add new pancake without adding syrup to top

• Count the number of sweet pancakes• Pancake is NOT sweet if none of the sides are sweet

Otherwise, the pancake is sweet• Limitation:• Only use Stack/Queue to solve the problem• STACK: Last In First Out• QUEUE: First In First Out

Alice the Baker• Input:• First line is [INTEGER] N and [INTEGER] Q

• N = initial #pancakes; Q = #queries/operations• Next Q-lines are [STRING] operations

• If operations is FLIP: contains another [INTEGER] index• Else only the operations is present

• Output:• Only output when COUNT operations is

encountered• Output: [INTEGER] <number of sweet pancakes>

followed by newline character

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

ABCDE

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

ABCDE

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

CDE

F

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

CDE

F

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

E

CD

F

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

E

CD

F

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

ECD

F

Alice the Baker• Discussions:• How do you Store a Pile of Pancake?

• Stack• Why?

• How do you Flip a Pile of Pancakes? Use of auxiliary Queue to reverse the order of a Stack

• How do you Count the Pile of Pancakes in Stack?• You cannot look into arbitrary position (violate Stack

structure)• You can only pop from the top, push to the top• You cannot use any other data structure besides

Stack/Queue Use of auxiliary Stack to preserve the order of a Stack

Stack/Queue Primer• Stack:• Last In First Out• Can be simulated using Linked List

• A restricted Linked List• Insert only to the beginning (i.e. push = addFirst)• Remove only from the beginning (i.e. pop =

removeFirst)• Queue:• First in First Out• Can be simulated using Linked List

• A restricted Linked List• Insert only to the end (i.e. enqueue = addLast)• Remove only from the beginning (i.e. dequeue =

removeFirst)

Stack/Queue Primer• Stack A Stack B Stack A• Remove all elements from Stack A Insert into B• Remove all elements from Stack B Insert into A• int size = A.size();for (int i=0; i<size; i++) B.addFirst(A.removeFirst());for (int i=0; i<size; i++) A.addFirst(B.removeFirst());

1 2 3 4 5

Stack/Queue Primer• Stack A Queue B Stack A• Remove all elements from Stack A Insert into B• Remove all elements from Queue B Insert into A• int size = A.size();for (int i=0; i<size; i++) B.addLast(A.removeFirst());for (int i=0; i<size; i++) A.addFirst(B.removeFirst());

1 2 3 4 5

Alice the Baker• Algorithms:• Flip the Pancake Pseudo-code:

• function Flip (index) { flip using auxiliary Queue; FOR each element moved back to Stack DO { IF needs sweeten THEN sweeten; } add sweetener to top stack;}

Alice the Baker• Algorithms:• Add to the top of the Pancake Pseudo-code:

• function Add() { push into Stack; }• Count the number of Sweet Pancake Pseudo-code:

• function Count() { push into auxiliary Stack; FOR each element moved back to Stack DO { IF sweet THEN increment counter; }}

THE ENDAny Questions?

top related