You are on page 1of 9

Assignment 2

Due Date: Monday, October 24th 2016, 11:59pm

Objectives

1. To give the students practice implementing concepts presented including:


a. Backtracking
b. Dynamic Programming
c. Greedy Algorithms

Deliverables

1. Analysis.pdf
a. Write up a brief description of your approach to each problem.
b. The write up should contain the Big-O analysis and a description of how that runtime
was determined.
c. Backtracking solutions should give a loose upper bound on the runtime and an intuitive
argument for the effectiveness of pruning.
d. Dynamic Programming solutions should discuss state and transitions.
e. Greedy algorithms should include an argument of correctness. (Though a more rigorous
proof is recommended.)
2. Solution.java
a. Each problem has a filename. For each problem include a solution using that filename.
For the problem XKCD, for example, include a Java file named XKCD.java. Any
additional classes used must be nested classes for the main solution class.
3. Solution.txt
a. Include a test file that meets the specification named as the filename. For the problem
XKCD, for example, include a text file named XKCD.txt.

Scoring

1. Half your points are allocated to your analysis write-up. Be sure to include it.
2. The other half of your points are evenly distributed between problems.
3. The scoring for your problems is based on the number of tests passed in each problems test set
plus the tests of your peers.
4. Bonus points are awarded based on the number of solutions your test case breaks proportioned
relative to the test case (student or instructor created) that breaks the most solutions. In the
event that all solutions work, the entire class will receive full bonus points.

The program should read from standard in (System.in in Java). Output the relevant output to Standard
Output (System.out in Java).

Problem A: XKCD
filename: XKCD
timelimit: 3 seconds

Matt is a fan of the web comic XKCD.


Being such a fan he would like to come
up with an online name similar to xkcd
(he wants to keep his lower-case). Your
job will be to help Matt come up with a
name similar to xkcd.
Looking at this name it can be seen that
taking the sum of the ordinals of each
letter in the word totals to 42 or, as we
know, the answer to life, the universe and everything. Ordinals are defined as the 1-based index for each
letter in the alphabet (a=1, b=2, c=3, , z=26).
Matt also remembered watching an interview with Randall Munroe (the creator of xkcd) and learning
one of the requirements of the online name he chose for himself: xkcd (xkcd the comic was later given
this same name). Randall required that his name be non-pronounceable. Therefore, Matt would also like
his name to be non-pronounceable as well. To ensure this, Matt would like to choose a name that does
not have any vowels in it. To be extra careful he would like to include the letter y as a vowel.
Lastly, Matt noticed that the values of each of the ordinals of the letters in xkcd are strictly decreasing
if you swap the last two letters (i.e., xkcd xkdc where = 24 > = 11 > = 4 > = 3). To make
his name even more similar to xkcd, Matt would like his name to have a strictly decreasing order when
the last letters are swapped.
Given these rules, Matt would like to see what names he could possibly choose from a list of names that
meet these requirements. Even though he is a huge fan of XKCD, he still wants to be unique, however.
He has decided to have one major difference in his chosen name. He would like to be able to look at
names of different lengths that meet his requirements. Your job is to write a program that takes in a
length desired and prints out all the names of that length in alphabetical order that meet his XKCD-like
requirements.
Given a name with a particular length, print out all names of that same length whose ordinals for each
character add up to 42, are strictly decreasing when the last letters are swapped and are nonpronounceable (meaning that the name contains no vowels including y).

Input

Your program will be run multiple times on multiple test files. Each test file will follow the following
constraints:
The first line of input will consist of a single integer (2 42) representing the length of the names
you must print out that are xkcd-like.

Output

Output lines where is the number of xkcd-like names of length . The names must appear in
alphabetical order. If no such names exist output Mostly Harmless instead.

Samples
6

42

Input

Output
kjhfcd
kjhgbd
ljgfcd
ljhfbd
ljhgbc
lkgfbd
lkhfbc
lkjdbc
mjgfbd
mjhfbc
mkgfbc
mlhdbc
nhgfcd
njgfbc
nkhdbc
nlgdbc
nmfdbc
phgfbc
pjgdbc
pkfdbc
qjfdbc
rhgdbc
shfdbc
tgfdbc
Mostly Harmless

Problem B: Longest Palindrome


filename: Palindrome
timelimit: 3 seconds

Matt loves palindromes. Palindromes are strings that read the same forward and reverse such as
tacocat or deleveled. Sometimes words are not palindromes and this makes Matt sad. That is why he
would like to know the longest palindrome he can make by only removing characters from the word. So
less awesome words like firefighter can be made into palindromes likes refer. The characters may only
be removed and not rearranged.

Input

Your program will be run multiple times on multiple test files. Each test file will follow the following
constraints:
The first line contains a single string (1 || 1,000) consisting of only lowercase characters.

Output

Output a single integer representing the length of the longest palindrome that can be formed by
removing characters.

Samples
tacocat
aaabaaba
firefighter

Input

Output
7
7
5

Problem C: Longest Palindrome 2


filename: Palindrome2
timelimit: 3 seconds

Matt loves palindromes but has made a horrible mistake. You see he created a problem for a class
assignment to covert strings that are not palindromes (words that are the same forward and reverse)
into strings that are palindromes by removing characters. But the major blunder is not requiring they
find the palindromes. They need only find the lengths. That is why he has created another problem that
is exactly the same but they must print out an example of such a palindrome. But not just any example!
He would like the palindrome to come first alphabetically out of all such palindromes.

Input

Your program will be run multiple times on multiple test files. Each test file will follow the following
constraints:
The first line contains a single string (1 || 1,000) consisting of only lowercase characters.

Output

Output a string which is the longest palindrome possible to form by removing characters but also comes
first alphabetically.

Samples
tacocat
aaabaaba
firefighter

Input

Output
tacocat
aaabaaa
refer

Problem D: Sticks
filename: Sticks
timelimit: 3 seconds

Matt found a big stick in his backyard after hurricane Matthew attacked. Thinking this would be an
excellent premise for a problem for his CS 2 class he proceeded to notice that pieces of the stick were
cut in perfect notches throughout.
Matt quickly realized that the stick can be broken along these notches. The cost, in effort units (which
would be explained except for requiring effort to explain), of breaking a stick along the notch is the
products of the lengths of the two sticks that form by breaking the stick. For example, if he is breaking a
stick of length 10 into a stick of length 3 and one of length 7, the total cost would be 3 7 = 21 effort
units. He can continue to break his stick (or growing collections of sticks) until there are no notches
remaining and he has a stockpile of sticks (with which he can arm squirrels and take over the university).
As Matt is a wimp in terms of strength and doesnt have amazing force powers like the long neck Jedi or
the short-necked Christmas Jedi, he would like to know the minimum amount of effort required to
completely break apart the original stick. The total effort is the sum of effort required for each break.

Input

Your program will be run multiple times on multiple test files. Each test file will follow the following
constraints:
The first line contains two integers and (1 200, 2 105 ) representing the number of
notches on the stick Matt found and the length of the stick, respectively.

The following line contains integers in strictly increasing order representing each location
(0 < < ) on the stick where notches exist. The endpoints of the stick are locations 0 and .

Output

Output a single integer representing the minimum effort required in effort units to arm his army of
squirrels.

Samples
3
1
4
1

6
3 4
1000
2 3 999

Input

Output
13
3990

Problem E: Dont Cross the Beams


filename: Beams
timelimit: 3 seconds

Lasers are great fun! That is why you have setup a configuration of posts placed in a circular
configuration with equal spacing. On each post you will place either a laser or a mirror box to receiver
the laser beam!
You havent quite figured out a configuration that looks nice but you do know for each post if you want
it to send or receive laser beams of the two types of colors available (red or blue). You also dont want
any of the beams to cross in your laser display.
Now you want to know the maximum number of lasers you can have in your design given a starting
configuration of red and blue posts. Below is an example of posts and lasers being sent between them.
Note that lasers travel in a straight line from their source to their mirror box.

Input

Your program will be run multiple times on multiple test files. Each test file will follow the following
constraints:
The first line of input is a string (1 || 105 ) of character R and B, representing blue or red posts
if you walked around the circle starting at an arbitrary post.

Output

Output a single integer representing the maximum number of posts you can pair of the same color and
not cross the laser beams.

Samples
RRRR
BBRBBRBRB
BRBRB
BBBRRRBBBRRR

Input

Output
2
4
2
5

Hints

1. Problem A
a. Be sure to analyze the runtime!
2. Problem B
a. (2 ) solution is possible.
3. Problem C
a. Be careful of your runtime for building the string!
b. StringBuilder is your friend.
4. Problem D
a. (3 ) solution is possible.
b. Does a faster greedy solution exist?
c. Do you have a clever case to break nave greedy solutions?
d. Be careful of integer overflow!
5. Problem E
a. () solution is possible.

You might also like