Cornell Programming Competition



Download 63.64 Kb.
Date conversion10.05.2017
Size63.64 Kb.


Cornell Programming Competition

Spring 1998

This semester, I decided to write problems that were all related to each other in some way. There is a common “story” thread that goes through all of the problems. While writing this story, I got a little carried away. So, there are 6 problems. I certainly don’t expect anyone to finish all the problems in 4 hours. However, I had a lot of fun writing the problems, and didn’t want to leave any out.
After every contest, I post my solutions, the test input, and the test output to the web. This semester, I will take one of the correct submissions and post that as the correct solution. Proper credit will be given to the author.
You are to read input from standard input and write output to standard output. The first line of input will (almost) always be a single non-negative integer. This integer will indicate the number of data sets that follow. You are to perform the same processing on each data set. (The only exception is Problem 4.)
It is not necessary to read in all the data sets before writing any output. I suggest you read in a data set, write its output, then move on to the next data set.

This page intentionally left blank.

Cornell Programming Competition

Spring 1998


Problem 1 – Going to the Chapel

As it happens, Adam is getting married this June. The wedding day is always very hectic, and Adam needs your help solving various problems that arise.



Description

It’s the wedding day, and Adam got up late. He needs to go from his apartment to the jeweler, the tailor, and the florist (in any order), and then to the chapel.

Given nodes and the time it takes to travel between them, give the route that Adam should take to reach the chapel as quickly as possible. Of course, since this is Ithaca, all of the routes are one-way.
Nodes are labeled by capital letters. Node “A” is Adam’s apartment; node “J” is the jeweler, node “T” is the tailor, node “F” the florist, and node “C” the chapel.

Example

B F
A T C


D J
In this map, the quickest route from A to C is ABTJCFC, and has length 20.

Input / Output

The first line of input will be a non-negative integer. This is the number of data sets that will follow.


Each data set is composed of one or more lines. The first line is a non-negative integer, n. The next n lines are the routes and their lengths. For example, “A B 5”, indicates that there is a route from node A to node B of length 5. Lengths are positive integers. Nodes are capital letters “A” through “Z”.
After reading each data set, compute the quickest route from node A to node C that also passes through nodes J, T, and F, in any order. Print out the data set number, the route, and its length. Use the format shown in the Sample Output, below.
Every data set will have a unique quickest path.

Sample




Sample Input

The first data set corresponds to the map given above. The comments on the right are not part of the actual input.


2 There will be 2 data sets

12 Data set 1: n = 12


A B 2 1: Route from node A to node B has length 2

A D 3 2: Route from node A to node D has length 3

B D 1


D B 1

B T 3


D T 4

T F 5


F J 7

J C 6


F C 1

T J 5

C F 3


6 Data set 2: n = 6

A B 1


B C 1

B F 1


F T 1

T J 1


J B 1

Sample Output

Data set 1: The shortest path is ABTJCFC and has length 20.

Data set 2: The shortest path is ABFTJBC and has length 6.
Cornell Programming Competition

Spring 1998


Problem 2 – Photographs


Description

Adam would like to get a picture of all of his guests. Since he’s a computer scientist, he wants the guests to be sorted by height. Unfortunately, he does not know the height of each guest. He does know, in some cases, which of two guests is taller. You are to read in a list of pairs of guests, indicating which of the two is taller, and output the order in which the guests should stand.



Input / Output

The first line of input is a non-negative integer. This indicates the number of data sets that will follow.

Each data set begins with two positive integers. The first integer will be n (the number of guests), and the second will be m (the number of pairs of guests that Adam knows the relative heights of). The guests are numbered 1 to n
. This will be followed by m lines. On each line will be two integers, each in the range of 1 to n. The first guest of each pair is taller than the second guest.

You are to output the order in which the guests should stand, shortest to tallest, all on one line. If the height information is inconsistent (i.e. guest 1 is taller than 2, and guest 2 is taller than 1), output “Data inconsistent.” If the height information is not sufficient to determine the order in which the guests should stand (i.e. 1 is taller than 2, and 1 is taller than 3, but no information is provided about the relative heights of guests 2 and 3), output “Not enough information.” Use the format shown in Sample Output, below.
There will be at most 100 guests. m is at most 200. You will not be given a data set which is both inconsistent and lacks sufficient information to determine the order.


Sample




Sample Input

The comments on the right are not part of the actual input.


3 There will be 3 data sets

5 4 Data set 1: n = 5, m = 4

1 2 1: guest 1 is taller than guest 2

2 3 2: guest 2 is taller than guest 3

3 4 3: guest 3 is taller than guest 4

4 5 4: guest 4 is taller than guest 5

3 3 Data set 2: n = 3, m = 3

1 2


2 3

3 1


50 2 Data set 3: n = 50, m = 2

19 24


15 3

Sample Output

Data set 1: 5 4 3 2 1

Data set 2: Data inconsistent.

Data set 3: Not enough information.

Cornell Programming Competition

Spring 1998


Problem 3 – Let Them Eat Cake


Description

There are a total of n people at the reception, and they all get in line for a piece of cake. We will number the people 1 to n, where person 1 is at the front of the line. Person 1 always takes a specified amount of cake. The other people in line eat an amount of cake that depends on the amount of cake eaten by those who went before them in line. The dependence is specified by the function below. The rules are followed in order: that is, if for example rule 2 and 3 both apply, then rule 2 is used.




  1. The first person in line always takes a specified amount of cake.

  2. If a person’s position in line is divisible evenly by 7, then they will take an amount of cake equal to their position in line modulo 11, plus 1. (We add one because otherwise person 77 would get no cake at all.)

  3. If a person’s position in line is a perfect square, he or she takes an amount of cake equal to the square root of their position.

  4. If the person’s position in line is even, he or she takes one more unit of cake than the previous person.

  5. If the previous person took no cake, then the person takes the same amount of cake as the first person.

  6. Otherwise, the person takes one fewer unit than the previous person.

Adam wants to know, given the n and the amount of cake the first person takes, how much total cake is eaten.


Example

Suppose there are 10 people in line, and the first person takes 1 unit of cake. The following chart shows how much cake each person takes, as well as which rule was used to determine how much cake he or she would take.



Person

1

2

3

4

5

6

7

8

9

10

Cake

1

2

1

2

1

2

8

9

3

4

Rule

1

4

6

3

6

4

2

4

3

4

A total of 33 units of cake are eaten.



Input / Output

The first line of input will be a single non-negative integer. It will indicate the number of data sets to follow.


Each data set will consist of two integers. The first, n, is positive, and indicates the number of people in line. The second, c, is non-negative, and indicates the amount of cake that the first person eats.

For each data set, output the total amount of cake that is eaten. Follow the format shown in Sample Output, below.

There will be at most 1000 people in line. (Adam is very popular.)


Sample




Sample Input

The first data set corresponds to the example, above.


3

10 1


5 10

1000 1


Sample Output

Data set 1: 33 units of cake are eaten.

Data set 2: 34 units of cake are eaten.

Data set 3: 7289 units of cake are eaten.


Cornell Programming Competition

Spring 1998


Problem 4Congratulations


Description

Adam’s uncle Sid is a spy. Because he is on an under-cover assignment in Rome, he is unable to make it to the wedding. However, he is able to smuggle Adam an encoded message. Several years ago, Sid gave Adam a list of passwords that Sid might use to send a message, in just such a situation.


You are to read a list of passwords, and several coded messages. For each coded message, figure out which password was used to encrypt the message. You can tell that a password is correct because the decrypted message will contain the word “CONGRATULATIONS”. (Both the code words and the message will be in all capital letters.)

Caesar Coding

Uncle Sid is in Rome, so he decided to encode the messages using Caesar coding. In Caesar coding, letters are treated as numbers. “A” is 1, “B” is 2, and so on, up to “Y” is 25, and “Z” is 26.

To decode a message, repeat the password over and over again until it is the same length as the encrypted message. For example, if the password is “CODE” and the message is “ZZNIBWH”, we would get “CODECOD” after repeating the password. Now, convert both the repeated password and the encrypted message into numbers, character by character. In our example, they become “3-15-4-5-3-15-4” and “26-26-14-9-2-23-8”. Add the two sequences of numbers together, number by number. If a number is greater than 26, they wrap around, so 27 becomes 1, 28 becomes 2, etc. The new sequence is “3-15-18-14-5-12-12”. Finally, convert these numbers back into letters. The decrypted message is “CORNELL”.


Input / Output

The first line of output will be two non-negative integers. The first will be n, the number of code words; the second will be m, the number of encrypted messages.


The next n lines will contain one password per line. Each password will be between 1 and 20 characters, all uppercase letters.
The next m lines will contain one encrypted message per line. Each message will be between 1 and 60 characters, all uppercase letters.
For each encrypted message, you are to determine which password correctly decrypts the message. You will know which password is correct because the decrypted message will contain the word “CONGRATULATIONS”, in all capital letters. Write which password worked, and the decrypted message. Follow the format shown in Sample Output, below.
Exactly one password will work for each message.

Sample




Sample Input

The comments to the right are not part of the actual input.


5 3 There are 5 passwords and 3 encrypted messages

ANT Password 1

BABYLON Password 2

CODE Password 3

DAY Password 4

ELEPHANT Password 5

ANLHFLFSKYUWZZQZBBA Message 1

XCIQJZFAGOOSGME Message 2

ZZNIBWHXLYCMXEQGXEEJKDJZMSAR Message 3

Sample Output

Message 1: Password BABYLON worked. Message is CONGRATULATIONSADAM.

Message 2: Password ELEPHANT worked. Message is CONGRATULATIONS.

Message 3: Password CODE worked. Message is CORNELLCONGRATULATIONSNEPHEW.

Cornell Programming Competition

Spring 1998


Problem 5 – Mischievous Children


Description

Adam’s parents put up a sign that says “CONGRATULATIONS”. The sign is so big that exactly one letter fits on each panel. Some of Adam’s younger cousins got bored during the reception and decided to rearrange the panels. How many unique ways can the panels be arranged (counting the original arrangement)?



Input / Output

The first line of input is a single non-negative integer. It indicates the number of data sets to follow.


Each data set consists of a single word, in all capital letters. For each word, output the number of unique ways that the letters can be rearranged (counting the original arrangement). Use the format shown in Sample Output, below.
Each word will have at most 20 letters. There will be no spaces or other punctuation.
The number of arrangements will always be able to fit into an unsigned long int. Note that 12! is the largest factorial that can fit into an unsigned long int.


Sample




Sample Input

3

HAPPY



WEDDING

ADAM


Sample Output

Data set 1: 60

Data set 2: 2520

Data set 3: 12


This page intentionally left blank.

Cornell Programming Competition

Spring 1998


Problem 6 – Shall We Dance?

Description

Adam wants every guest to dance with every other guest at the reception (even if the two guests are of the same gender). You are to read in the number of guests, and output the dance schedule.

The dance schedule shows which guest dances with which other guest during each song. A valid dance schedule must satisfy the following two properties:


  1. During a song s, each guest can dance with at most one other guest.

  2. During a song s
    , if guest i dances with guest j, then guest j must dance with guest i.

Further, Adam wants to use the minimum number of songs necessary so that every guest dances with every other guest. Suppose the number of guests is n. Then if n is odd, n songs are required for every guest to dance with every other guest; if n is even, then n-1 songs are required.


If the number of guests is odd, then obviously all the guests cannot be paired up. Any guest who is not dancing with another guest simply dances with himself (or herself).

Dance Card

A dance card is a square matrix. The rows correspond to songs, and columns correspond to guests. If the entry in row s, column i, is j, this means that during song s, guest i dances with guest j. See below for an example of a dance card.



Example

Suppose there are 5 guests. Then 5 songs are required. The dance card below shows which guest every guest dances with during each of the 5 songs. During each song, exactly one guest is not paired, and dances with himself.










G

U

E

S

T








1

2

3

4

5

S

1

5

4

3

2

1

O

2

1

5

4

3

2

N

3

2

1

5

4

3

G

4

3

2

1

5

4




5

4

3

2

1

5

During the first song, guests 1 and 5 dance together, guests 2 and 4 dance together, and guest 3 dances with himself. During the second song, guests 2 and 5 are together, guests 3 and 4 are together, and guest 1 is alone.

Input / Output

The first line will be a single non-negative integer. This indicates the number of data sets that will follow.

Each data set consists of a single positive integer, n, indicating the number of guests. For each data set, you are to print out the dance card, in the format shown in the Example above and Sample Output below. Each row corresponds to a song, and each column corresponds to a guest. If the entry in row s column i is j, this indicates that during song s, guest i dances with guest j.

The guests are numbered 1 to n. If a guest is not paired during a song, then he dances with himself. When printing the dance card, use a command similar to printf(“%3d”, m); so that the columns line up neatly. There should be exactly one blank line between data sets. Exactly one blank line should follow the last data set.


There are many different possible dance cards for n guests. (In fact, well over n! of them.) Any correct dance card will be accepted. This problem will not be graded by hand; it will be graded using another program. Therefore, it is vital that you follow the output specifications exactly. (If our program says that the output of your program is incorrect, we will look at your output manually.)
There are at most 20 guests.


Sample




Sample Input

2

5



6

Sample Output

The notes to the right are not part of the actual output.

Data set 1:

5 4 3 2 1

1 5 4 3 2

2 1 5 4 3

3 2 1 5 4

4 3 2 1 5

Blank line

Data set 2:

5 4 6 2 1 3

6 5 4 3 2 1

2 1 5 6 3 4

3 6 1 5 4 2

4 3 2 1 6 5

Blank line









The database is protected by copyright ©hestories.info 2017
send message

    Main page