26-CS106X-Practice-Midterm

# String restorestringstring str mapchar entitymap

This preview shows pages 4–6. Sign up to view the full content.

string RestoreString(string str, Map<char>& entityMap) { Problem 3: Pascal’s Travels You’re given a grid of positive integers to represent a game board, where the [0, 0] entry is the upper left corner. The number in each location is the number of squares you can advance in any of the four primary compass directions, provided that move doesn’t take you off the board. You’re interested in the total number of distinct ways one could travel from the upper left corner to the lower right corner, given the constraint that no single path should ever visit the same location twice. Consider the initial game board to the left, and notice that the upper left corner is occupied by a 2. That means you can take either two steps to the right, or two steps down (but not two steps to the left or above, because that would carry you off the board). Suppose you opt to go right so that you find yourself in the configuration to the right. From there, you could continue along as follows: So, this series of moves illustrates just one of potentially several paths you could take from upper left to lower right. Your job here is to write a function called NumPaths , which takes a Grid of integers and computes the total number of ways to travel to the lower right corner of the board. Note that you never want to count the same path twice, but two paths are considered to be distinct even if they share a common sub-path. And because you want to prevent cycles, you should change the value at any given location to a zero as a way of marking that you’ve been there. Just be sure to restore the original value as you exit the recursive call. You’ll want to write a wrapper function and some utility functions to decide it you’re on the board or not. int NumPaths(Grid<int>& board) { 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Problem 4: Longest Increasing Subsequence Given a string of lowercase letters, write a function LongestIncreasingSubsequence that returns the longest increasing subsequence. Recall that a subsequence of a string is the same string, except that an arbitrary number of its characters have been removed (and the order of the characters that remain is preserved.) By increasing, we mean that as a character sequence the string is sorted from low to high. Given the string "aiemckgobjfndlhp" , each of the following is a subsequence: "mjp" "acgjlp" "aiemckgobjfndlhp" "aemckobjfnlp" "emjfnp" "aegjnp" Of the six subsequences above, only "acgjlp" and "aegjnp" are increasing, since as character arrays they are sorted from low to high. And because there aren’t any increasing subsequences of length 7 or more (an exhaustive search demonstrates this—just trust us), "acgjlp" and "aegjnp" are each longest increasing subsequences. Using this and the next page, write a recursive function that takes a string and returns the longest increasing subsequence. If there are two or more such subsequences, your function can return any one of them.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern