{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


29S-CS106X-Solution - p CS106X Winter 2008 Handout 29S...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
p CS106X Handout 29S Winter 2008 February 25, 2008 CS106X Midterm Solution Problem 1: Sanitizing Strings (10 points) /** * Function: Sanitize * ------------------ * Accepts the specified string and the collection * of banned substrings, and returns the smallest string * that can be generated by the optimal application of * substring eliminations. */ string Sanitize(string workingString, Vector<string>& substrings) { string shortest = workingString; for (int i = 0; i < substrings.size(); i++) { string substring = substrings[i]; int found = 0; while (true) { found = workingString.find(substring, found); if (found == string::npos) break; string reduction = Sanitize(workingString.substr(0, found) + workingString.substr(found + substring.size()), substrings); if (reduction.size() < shortest.size()) shortest = reduction; found = found + 1; } } return shortest; } Criteria for Problem 1 (out of 10 points) Provides either an explicit or (as above) implicit base case: 1 point. Considers all substrings via a for or while loop: 1 point At the very least searches for each substring at least once: 2 points o Recognizes the need to call find , and executes the first call for any given substring properly: 2 points o Recognizes this, but doesn’t deal with find or find ’s return value properly: 1 point Correctly recurses for each occurrence of any given substring, including the second, third, and fourth instances: 2 points o Recognizes and properly handles it: 2 points o At least recognizes the need for the inner while loop, but doesn’t execute it properly: 1 point o Doesn’t even recognize the need in the first place: 0 points Makes correct recursive call: 2 points Catches the return value: 1 point Conditionally updates running minimum as I have above: 1 point
Background image of page 1

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

View Full Document Right Arrow Icon
2 Problem 2: Recursive Backtracking and the 70 2 Puzzle (10 points) It’s a little known fact that the sum of the first 24 positive squares—that is, 1 + 4 + 9 + … + 23 2 + 24 2 —is equal to 4900, which just happens to be the square of 70. Naturally, this leads to the question of whether or not 24 square tiles of area 1, 4, 9, 16, and so forth can be laid down to completely cover a 70 x 70 board. Here’s a partial tiling of a 70 x 70 board where tiles with sides of unit lengths 15 – 24 have been placed, but tiles with sides of length 1 – 14 have not. Your job is to use recursive backtracking to determine whether or not such a tiling exists. You should assume that the following functions exist and operate as described: // returns true if and only if a square of the specified dimension could be // placed without overlapping the edges or any previously placed squares such // that the lower left corner of the tile covers the cell at (row, col) CanPlaceSqaure(Grid<bool>& board, int dimension, int row, int col); // Updates the board to reflect that a square tile of the specified dimension // has been placed such that its lower left corner covers the cell at (row, col) PlaceSquare(Grid<bool>& board, int dimension, int row, int col); // Undoes the actions associated with the corresponding call to PlaceSquare.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

29S-CS106X-Solution - p CS106X Winter 2008 Handout 29S...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online