Westminster College 2011 High School Programming Contest October 24, 2011 Rules: 1. There are six questions to be completed in two and 1/2 hours. 2. All questions require you to read the test data from standard input and write results to standard output. You should not use files for input or output. 3. The allowed programming languages are C++ and Java. 4. All programs will be re-compiled prior to testing with the judges’ data. 5. Programming style is not considered in this contest. You are free to code in whatever style you prefer. Documentation is not required. 6. Judges’ decisions are final. No cheating will be tolerated. 2011 Westminster High School Programming Contest Problem A: 1 Decryption for Dummies You’ve just intercepted an encrypted string of characters from your arch nemesis Archie Nemesis. He uses a simple substitution cipher to convert a message to an encrypted string, where every letter in the message is replaced with a (potentially) different and unique letter. So for example, all A’s in the message might be changed to R’s, all B’s to Z’s, etc. You also have in your possession a list of possible messages that Archie might send - messages like “ATTACK” or “RETREAT” or “GOTMILK”. Your job is to determine if the encrypted string matches any of these possible messages. For example, if the encrypted string was “SNQKIPA”, it couldn’t be the encrypted version of “ATTACK” (not the correct length) or of “RETREAT” (since R, E and T would each be encrypted to two different letters which isn’t allowed) but it could be the encrypted version of “GOTMILK” with the substitution G→S,O→N,T→Q,M→K,I→I,L→P, and K→A. Your job is to write a program to automate this process of finding matches. And I’m sorry I called you a dummy. Input There will be multiple test cases. Each test case will start with a line containing a positive integer n and a string s, where s is the encrypted string and n is the number of possible messages. The next n lines will contain each of the n messages, one per line. Each message and the encrypted string will consist only of uppercase letters. The maximum value for n is 50. Input will terminate with a line containing 0 DONE. Output For each test case, output the case number followed by all possible messages that match the encrypted string. If there is more than one matching string, print them in the order that they appear in the test case, separating each by a single blank. Sample Input 3 SNQKIPA ATTACK RETREAT GOTMILK 1 AAAA STOP 4 ABCD NORTH SOUTH EAST WEST 0 DONE Sample Output Case 1: GOTMILK Case 2: Case 3: EAST WEST 2011 Westminster High School Programming Contest Problem B: 2 Draw! We can simulate a very simple drawing program as follows: The drawing surface will be an n × m grid of squares (n columns, m rows), all squares initially white. Squares can be colored in black with a pen which is initially located at (0, 0), which corresponds to the lower left corner of the grid. The pen responds to the following two commands: move(dx, dy) - move the pen (without drawing) to the location dx columns away and dy rows away from its current location. For example, if the pen is located at location (20, 6) and the command move(4,3) is given, the pen is moved to location (24, 9). From here, the command move(-8,0) would move the pen to location (16, 9). If any move command would move the pen off the grid, it is ignored and the pen stays where it is. draw(dx,dy) - draw a line from the current location (x,y) to (x+dx, y+dy). Afterwards the pen is located at (x+dx, y+dy). To simplify things, dx and dy will only be set so that the pen moves either N, NE, E, SE, etc. In other words, either 1) dx and/or dy will be 0, or 2) dx = dy, or 3) dx = -dy. If a draw command would move the pen off the grid, the pen draws a line until it hits the edge of the grid and then stops there. The before and after images below show the result of the following 5 commands on a 5 × 6 grid: move(1,3), draw(2,0), draw(-2,2), move(3,-4), draw(0,-5) 5 5 4 4 3 3 2 2 1 1 0 * Pen 0 0 1 2 3 4 Pen 0 1 2 3 4 Note that the last draw command does not draw the complete line, stopping the pen at location (4,0). Note also that the command draw(0,0) will blacken the current square the pen is on without moving the pen. Your task (if you haven’t already guessed) is to process of a series of these commands and draw the resulting picture. Input The input file will consist of multiple test cases. Each case starts with three positive integers n m c indicating the number of columns (n) and rows (m) in the grid and the number of commands to process (c). The maximum values for n and m is 25. Following this will be c lines of the form move dx dy or draw dx dy. A line with three zeros will terminate input. (over) 2011 Westminster High School Programming Contest 3 Output For each test case, output the case number on a single line and on the next m lines draw the resulting picture. Each of these lines should contain n characters, each character either a ‘.’ if the corresponding grid square is white or an ‘X’ if the corresponding grid square is black. The first character on the last output line for each test case should correspond to location (0, 0) in the grid. Sample Input 5 6 5 move 1 3 draw 2 0 draw -2 2 move 3 -4 draw 0 -5 10 2 1 draw 0 0 0 0 0 Sample Output Case 1: .X... ..X.. .XXX. ..... ....X ....X Case 2: .......... X......... 2011 Westminster High School Programming Contest Problem C: 4 For the Birds Robin J. Flicker is an ornithologist who likes to keep track of the birds in her back yard. Every 5 minutes she checks a certain branch and notes the species of bird she see there (if any). She records all of this in a book she keeps by the window, using a single letter to identify a species or a period to indicate that no bird was seen. For example, after one hour she might write down something like "afa.dd.p..Pz", indicating that she saw species a at the first and third observations, species f at the second observation, no bird at the fourth, seventh, ninth and tenth observations, and so on. Note that she uses both lower and upper case letters, so she saw two different species at observations 8 and 11. One aspect of bird behavior that most interests Robin is the frequency with which species will return to her yard. Specifically, she would like to know which species had the longest time interval between consecutive sightings. In the example above, this would be species a which has an interval of length 2 between two sightings. If on the thirteenth sighting she had seen species d, then that species would have the longest interval, with a difference of 7 between this sighting and the sighting at observation 6. Input The input file will consist of multiple test cases. The file will start with a single integer n indicating the number of test cases. Each test case will consist of a single string containing upper and lower case letters and periods. Each case will take up exactly one line. Output For each test case, output species with the longest time interval between consecutive sightings along with the time interval. If any species are tied for the longest interval, print them all out alphabetically (all lower case first, followed by upper case), using the format shown below. There will always be at least one species that appears twice in each test case. Sample Input 4 afa.dd.p..Pz afa.dd.p..Pzd hWHrhWHrhWHr a.a.a.a.a.a.a.a.b..b Sample Output a: 2 d: 7 hrHW: 4 b: 3 2011 Westminster High School Programming Contest Problem D: 5 Living the Jai Alai(f ) Jai Alai is an indoor sport similar to racquetball which is popular in the east coast, especially Florida. A typical jai alai game involves 8 players who play one-on-one matches as follows: The players are placed on a list in some initial order - let’s assume the initial list is A, B, ..., H. The first match is between the first two players on the list (A and B in our example). The loser moves to the end of the list of players, while the winner (who we’ll call the current winner) gets to play the next person on the list (who we’ll call the newcomer); the loser of this game goes to the end of the list, and so on. We can characterize how a game progresses simply by indicating for each match whether the current winner won again or whether the newcomer won (we’ll assume in the very first game that B is the newcomer). So for example, if we were told that in the first 4 games the winners were current, newcomer, newcomer, current, the list of players would change as follows: Initial ABCDEFGH Winner = current ACDEFGHB newcomer CDEFGHBA newcomer DEFGHBAC current DFGHBACE In the first match, the current winner A wins, so the newcomer B goes to the end of the list. In the second match the newcomer C wins, so A goes to the end (and now C becomes the current winner). In the third match the newcomer D wins so C goes to the end, and in the final match the current winner (now D) wins, so the newcomer E goes to the end of the list. You job for this problem is to read in a list describing who won a set of matches and then output the final list. Input The input file will consist of multiple test cases. Each test case will consist of two lines. The first line will contain two positive integers m n, indicating the number of players (2 ≤ m ≤ 26) and the number of matches in the game (n ≤ 50). The initial list of players is the first m letters of the alphabet (uppercase). The next line will contain a string consisting of the characters ‘C’ and ‘N’; the first character specifies the winner of the first match (‘C’ = current winner, ‘N’ = newcomer), the second character specifies the winner of the second match, and so on. A line containing 0 0 will terminate input. Output For each test case output the final list of candidates, using the format shown below Sample Input 8 4 CNNC 26 1 N 0 0 Sample Output DFGHBACE BCDEFGHIJKLMNOPQRSTUVWXYZA 2011 Westminster High School Programming Contest Problem E: 6 Longing for Division OK, we’ll make this second-to-last problem short and sweet: if I divide 233 by 13, what’s the 101st digit after the decimal point? Well, lets make it a little harder. If I give you numbers n, m and d, determine the dth digit after the decimal point when n is divided by m. Input The input file will consist of multiple test cases. Each case will consist of the three positive integers n m d as described above. All of these values will be ≤ 10, 000. The input file will terminate with the line 0 0 0. Output For each test case output the case number followed by the requested digit. Note that if the input was 1 2 10 the correct answer would be 0, as we treat 1/2 as 0.5000000000 . . .. Sample Input 233 1 2 1 2 0 0 13 101 1 10 0 Sample Output Case 1: 7 Case 2: 5 Case 3: 0 2011 Westminster High School Programming Contest Problem F: 7 Right Visitations Willie lives in New Wilmington and has four friends: Ernie, who lives in Erie; Pete, who lives in Pittsburgh; Herman, who lives in Hermitage; and d’Artagnan, who lives in Youngstown. The map below shows the travel distances between these five cities. Willie has a long weekend coming up and wants to visit each of his friends. He doesn’t care which order he visits them as long as he visits each city exactly once, ending up back in New Wilmington. He would also like to save money on gas, so he wants to take the shortest possible trip. In the above example, he could go from NW to H to Y to P to E and back to NW for a total trip distance of 12+24+67+128+82 = 313 miles, but the best trip would go from NW to H to E to P to Y and back to NW for a trip distance of 12+81+128+67+18 = 306 miles (of course, the same trip but in reverse order would also be a minimum cost trip). Since Willie could possibly gain new friends and lose current ones (d’Artagnan is a little weird), he would like to have a program to determine this minimum distance given any five cities. Input The input file will consist of multiple test cases. The first line will contain an integer n indicating the number of test cases. Each test case will consist of four lines corresponding to the five cities which we will call A, B, C, D and E. The first line gives the distances from city A to cities B, C, D and E. The second line gives the distances from city B to cities C, D and E. The third line gives the distances from city C to D and E; and the fourth the distance from D to E. You may assume that Willie lives in city A (though in reality it doesn’t really matter which city he starts from). Output For each test case output the case number followed by the minimum round trip distance. (over) 2011 Westminster High School Programming Contest Sample Input 2 82 60 18 12 128 101 81 67 75 24 1 1 1 1 1 1 1 1 1 1 Sample Output Case 1: 306 Case 2: 5 8