26-CS106X-Practice-Midterm

String str problem 5 polydivisible numbers

Info icon This preview shows pages 5–8. Sign up to view the full content.

View Full Document Right Arrow Icon
string LongestIncreasingSubsequence(string str) { Problem 5: Polydivisible Numbers Polydivisible numbers are positive integers such that the first k digits, when taken as a number, are divisible by k, for all reasonable values of k. For example, 8076 is a polydivisible number, because: 8 is divisible by 1 80 is divisible by 2 807 is divisible by 3 8076 is divisible by 4 Other examples of polydivisible numbers: 30080, 1652588, 444402009, 80480408404, and 76245056107220 are all examples of polydivisible numbers. 3608528850368400786036725, at 25 digits, is the largest polydivisible number. Write a recursive procedure generatePolydivisibleNumbers that procedurally constructs all polydivisible numbers and returns them in a Set<int> . You should assume 5
Image of page 5

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

View Full Document Right Arrow Icon
there’s no limit to how big an int can be, and you can also assume that the number of polydivisible numbers is finite. Your implementation must make direct, relevant use of the observation that 10 * n + d (where 0 <= d < 10) can only be polydivisible if n is. Use this and the next page for your implementation: Set<int> generatePolydivisibleNumbers() { Problem 6: Sanitizing Strings Write a recursive procedure called Sanitize , which takes a string of text and a Vector<string> of banned substrings and returns the shortest string that can be generated by eliminating all illegal substrings. For instance, if the only banned substring is abc , then the string babaabcbcc can be sanitized down to b , via: babaabc bcc bababc c babc b Note that each string in the stack is the same at the one above it, except that the underlined substrings have been spliced out. A more elaborate example illustrates that baacabacaaabcaaabbaa can be edited down to baa if the set of banned substrings includes ac , ab , and caa , as illustrated by: baacabacaaabcaaab baa baacab acaaabcaabaa baacacaa abcaabaa baacaa bcaabaa baabcaaba a baab caaa bac aaa bab aa baa Write a recursive procedure called Sanitize that computes and returns the smallest string that can be generated by an optimal series of substring eliminations. 6
Image of page 6
Recall that the string ’s find method searches the receiving string from start forward and returns the smallest index where substring can be found (or string::npos if substring isn’t present.) int string::find(string substring, int start = 0); string Sanitize(string str, Vector<string>& substrings) { Problem 7: Recursive Backtracking and Scheduling Movies The Academy Awards are fast approaching, and you’ve been so busy with school and summer internships that you’ve not seen a single movie all year. You’ve decided to set aside a single Saturday to see a predefined list of nominated movies, and you’re hoping there’s some way to fit them all in on any given day. Given a dictionary of movies (each bundling the title, the length in minutes, and its various start times into a single struct ) and the titles you want to see, write a function that decides whether or not it’s possible to see everything you want to see in any given day. Of course, you can never be in two different theaters at the same time, but for simplicity, assume that you can attend a movie that begins at the same time another movie ends.
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern