CPS 149, Problems for Week 5


Question 1 There's a Hole in the Bucket

put caption here...

You are likely familiar with the story of Henry an Liza and the hole in the bucket. Not too many people know what happened after Henry managed to fix the hole in his bucket.

Liza, after the hole affair, was pretty upset with Henry. In an effort to get him to think, she had him fill a barrel with several different buckets. The barrel had to be filled exactly to the top by whole (or is that hole?) bucket quantities. Henry, being lazy, kept thinking about it until he could do it in the fewest buckets possible.

The Input:

The input will be a series of lines. The first line will contain a single number representing the size of the barrel in litres. The second and following lines each contain a single number representing the sizes of the available buckets, in litres.

The Output:

The output will be a single number representing the fewest buckets it takes to fill the barrel.
Sample Input A:		Sample Input B:		Sample Input C:
17			20			36
1			7			19
2			5			9
4			3			5
5			1			2
						1

Sample Output A:	Sample Output B:	Sample Output C:
4			4			4

Limits:

The barrel will never be greater than 40 litres. The pails will never be larger than the barrel, nor will there be more than 10 pails.

Question 2 Graph Colouring

Painting by Numbers...

Definition: Graph
A graph G is a finite set, V(G), of objects called ``vertices'', together with a set, E(G), of unordered pairs of distinct vertices. The elements of E(G) are called ``edges''.
Definition: Graph Colouring
A graph colouring is a finite set, C, of colours and vertex labelling function, w:V(G) -> C such that for all (u,v) in E(G), w(u) != w(v).
Definition: Chromatic Number
The chromatic number of a graph is the smallest, integer size of a set of colours that can colour that graph.

Problem:

Given a graph, output its chromatic number.

The Input:

Each graph is a list of edges separated by semi-colons ``;'', and ending with a blank edge (i.e. a double semi-colon, ``;;''. Each edge shall be a comma ``,'' separated pair of vertices, labelled by a positive integer. Any amount of white space may occur between numbers and punctuation.

Example, this is a square:

1, 2; 2, 3; 3, 4; 4, 1;;

The Output:

A single integer which is the chromatic number of the graph.

For the square above, the answer is ``2''.


Question 3 Run-Length Encoding

Simple compression...

The problem:

Many data files (such as pictures) are characterised by large runs of repeated data. These files can be compressed using a technique called run-length encoding (RLE). For this problem, you will need to implement a variant of RLE.

The basic idea behind RLE is to replace, say, 20 consecutive occurrences of the character `.', with the number 20 and a single `.'. This variant allows runs to be longer than one character. For example, the sequence abcabcabcabcabc should be converted into 0503abc. The first two characters, 05, are a pair of digits specifying how many times the pattern is repeated; the next two characters, 03, are the length of the repeated pattern, and that many characters following (abc) is the pattern to be repeated, itself.

Each compression saves a certain number of characters. The digits are not to be counted in considering the length of the compressed sequence. In the example above, 12 characters were saved. Each string to be compressed will probably consist of more than one repeated pattern. For example, the string abcdabcdabcdefefefefgggggg should turn into 0304abcd0402ef0601g.

When compressing a string, always start by compressing the repeated sequence that saves the most characters first, and then compress what's left over on the left, and on the right. For example, the string aabbaabbbbaabb should be compressed as 0201a0206bbaabb and not as 0204aabb0106bbaabb or 0204aabb0202bb0202aa0202bb. If more than one such sequence exists, pick the longer one. If more than one have the same length, pick the one further to the right.

If a string has no repeating sequences, output it with no digits in front.

You will receive a number of lines of text on standard input. These lines will not contain digits. You are to compress each one as described above, and write your answer to standard output. Use one line of output for each line of input.

Sample Input:

abcdabcdabcdefefefefgggggg
aabbaabbbbaabb
bananananas fors fors
aaaaa a a aaaaaa
aaaaa a a aaaa

Sample Output:

0304abcd0402ef0601g
0201a0206bbaabb
ba0402nas0205 fors
0401a0302a 0601a
0501a0302 a0301a