Westminster College 2009 High School Programming Contest October 6, 2009 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 les 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 nal. No cheating will be tolerated. 2009 Westminster High School Programming Contest Problem A: 1 Challenge 24 Lite You're all familiar with Challenge 24: you're given 4 numbers and have to nd a way to obtain the value 24 using the four standard arithmetic operators. For example, if the numbers are 5, 7, 7 and 5 you can get 24 several ways: (5+7)*(7-5), 5*5 - 7/7, and 7*7-5*5. (By the way, it's fun to play this game in the car using Pennsylvania license plates.) We'll simplify this game a bit by requiring that you must use the numbers left to right in the order given. In addition (no pun intended) the operators that are inserted between the numbers must be used in order from left to right as well. In other words, you must use the operator between the rst two numbers rst, then the operator between this result and the third number, and then the last operator. Using these rules, none of the above solutions for 5, 7, 7, 5 work, but the following does: 5+7+7+5. As another example, consider 4, 1, 7, 3. The solution 7*4-3-1 is no good (the numbers are not in the given order) and the solution 4-1+(7*3) is no good (since the operators are not performed left-to-right), but the solution 4-1*7+3 is ne. Note that this example does not use the normal precedence rules for the arithmetic operators, but that's ne: according to our rules you do the - rst, then the * and nally the +. Input The input le will consist of multiple test cases, each test case consisting of one line. Each line will contain four positive integers < 10. A line consisting of a four 0's indicates end-of-input. Output For each test case output the appropriate arithmetic expression if one exists, or the phrase \No solution exists." If more than one arithmetic expression exists, you can output any one of them. For example, for the input 8,3,7,7 you could output 8*3+7-7, 8*3-7+7,8*3/7*7, or 8*3*7/7. Sample Input 5 4 1 0 7 1 1 0 7 7 1 0 5 3 1 0 Sample Output 5+7+7+5 4-1*7+3 No solution exists. 2009 Westminster High School Programming Contest Problem B: 2 Dart-Ed Red, Ned, Ted and Ed are playing darts using the following simple dartboard: sB @@ s@ @ s @s @ @s s F D E A A: (-10,-10) B: (10,10) C: (0,-8) D: (8,0) E: (-2,-2) F: (2,2) C The outer and inner squares are centered at the origin and have side lengths 20 and 4. The center diamond is also centered at the origin, and has two vertices at (0,-8) and (8,0) (we'll let you gure out the other two points). A dart which lands in or on the inner square is awarded 5 points; darts outside of this square but within or on the diamond shape are awarded three points; darts outside of the diamond but within or on the outer square are awarded 1 point; all other darts score 0. The object (obviously) is to score the most points. While these four boys are adept at tossing darts, the mysteries of arithmetic escape them (one too many darts in the temples), so they need your help (and some Band-Aids). Input The input le will consist of multiple test cases. Each test case will start with a positive integer n which indicates the number of rounds in the game, where a round consists of one toss by each player. The next n lines will each contain 8 oating point values which indicate the locations of the 4 darts thrown in each round. The rst two values will be the x and y coordinates for Red's throw, the next two will be the x and y coordinates of Ned's throw. Following this will be the locations of the throws by Ted and Ed. A line containing a single 0 will terminate input. Output For each test case output the names and scores of the players in descending order of scores. If two or more people have the same score, output them in alphabetical order. Use the format shown in the example below. Sample Input 4 -6.14 -10.75 9.05 0.57 0.21 -1.29 8.29 -2.26 5.87 -7.37 -2.72 10.52 -1.07 1.07 3.11 1.79 -3.99 -2.66 1.41 -3.72 -0.91 -6.18 -0.36 3.34 6.89 -0.88 0.02 -1.89 -7.89 9.22 -8.32 -3.46 1 0.00 8.00 0.00 8.00 0.00 8.00 0.00 8.00 0 Sample Output Game 1: Ted - 14, Ned - 9, Ed - 8, Red - 7 Game 2: Ed - 3, Ned - 3, Red - 3, Ted - 3 3 2009 Westminster High School Programming Contest Problem C: Find the Snake You've just been handed a picture of a eld and have been asked to look for a snake. The picture is divided up into a square grid, and each grid contains either an 'S' indicating part of a snake, or some other character indicating some other item usually found in a eld (grass, bugs, cow pies, etc). Once you nd a snake, you need to determine where it starts and ends and how long it is, which for this problem means how many grid squares it occupies. The squares containing the snake will always be connected north-south or east-west (never diagonally) and all snake squares other than the rst and last will be connected with exactly two other snake squares (i.e., the snake will never wind back over or next to itself). For example, in the 6 5 grid below there is a snake of length 16, with endpoints at squares (1,2) and (5,5) (note that a lower case 's' is not considered part of a snake). 1 2 3 4 5 6 1 p o S S S k 2 S f S o S S 3 S e S r a S 4 S S S s S S 5 r s s n S e Input The input le will consist of multiple test cases. Each case starts with two positive integers n m indicating the number of rows and columns in the grid. There will then follow n lines each containing exactly m alphabetic characters. The rst of these lines will be row 0, the second row 1, etc. The maximum value for n and m is 20. Each test case will have at most one snake in it. A line with two zeros will terminate input. Output For each test case, output the two endpoints of the snake and the length of the snake, using the format shown in the example. Each endpoint should be output as (row,column). Always output the endpoint with the lower row number rst; in case of a tie, output the one with the lower column number. Note that the endpoints of a snake of length 1 will be the same. If no snake is found, output The field is snake-free. Sample Input 6 5 pSSSr ofeSs SSSSs Sorsn SSaSS pSSSe 2 2 ab cd 0 0 Sample Output Snake of length 16 found between (1,2) and (5,5). The field is snake-free. 2009 Westminster High School Programming Contest Problem D: 4 Knaster of Your Domain The Knaster Inheritance Method is a commonly used algorithm to divide up \indivisible" items among two or more people. For example, suppose Great Uncle Fritz has passed away and left his grand piano to his ve great nephews and nieces: Abigail, Boris, Clyde, Drusilla and Erving. Clearly you can't divide up this piano between the inheritors (\You get the rst octave, I'll get the pedals...") so we need some other way to ensure that everyone gets at least 1/5 of what they think the piano is worth. Here's how the Knaster system works: Everyone write down how much they think the piano is worth (we'll call this their bid ) and then they all reveal their bids at once. Then the following steps are taken: 1. The person with the highest bid gets the piano and pays 4/5 of what he/she bid. This money is put aside in a cash pool. 2. Each of the other persons takes 1/5 of their bid out of the cash pool. 3. Any remaining money in the pool is divided equally among all 5 people. The upshot is that the person who got the piano gets 1/5 of it free (since he/she only paid 4/5 of what they thought it was worth) while everyone else gets 1/5 of what they thought the piano was worth in cash (actually, everyone does a little better than that, since there's usually money to split in step 3). For example, suppose Abigail bid $30,000, Boris $24,000, Clyde $35,000, Drusilla $20,000 and Erving $31,000. Clyde would get the piano and pay only $28,000 (4/5 of his $35,000 bid). The other would get 1/5 of their bid out of this $28,000: Abigail - $6,000, Boris - $4,800, Drusilla - $4,000, and Erving - $6,200. This would leave $7,000 left in the pool, which would be split 5 ways - $1,400 for each. The nal tally: Clyde gets the piano and pays $26,600 (his original payment of $28,000 less the $1,400 he got back in step 3); Abigail gets $7,400, Boris gets $6,200, Drusilla gets $5,400, and Erving get $7,600. This method is easily modied to work with any number of inheritors. If you had four inheritors, then the person who gets the item would pay 3/4 of his/her bid, while the others would get 1/4 of their bid in cash; if you had 6 inheritors, then the person who get the item would pay 5/6 of his/her bid, while the others would get 1/6 of their bid in cash, and so on. Where do you come in? You inherit the job of automating this process. Input There will be multiple test cases. The rst line of each case contains an integer n (indicating the number of inheritors) follows by the one-word name of the item being inherited. The next n lines will each contain the name of an inheritor (which will always be one word) and their integer bid. There will always be a unique maximum bid and all cash values used in each test case (including the remaining cash used in step 3) will be multiples of n. The value of n will be between 2 and 10, inclusive. Input will terminate with a line containing a single 0. (over) 2009 Westminster High School Programming Contest 5 Output For each test case, output the results of the Knaster Inheritance Method in the following format: Case m: <name1> got the <item> and paid $p1. <name2> got $p2. <name3> got $p3. ... <name-n> got $p-n. where m is the case number (starting at 1), <name1> is the name of the person who got the item, paying <p1> dollars, and <name2>, <name2>, ... are the remaining inheritors (in the order they were given in the test case) and <p2>, <p3>, ... are the amounts of money they got. End the output of each test case with a blank line. Sample Input 5 piano Abigail 30000 Boris 24000 Clyde 35000 Drusilla 20000 Erving 31000 3 artificial_arm Tom 210 Dick 360 Harold 60 0 Sample Output Case 1: Clyde got the piano and paid $26600. Abigail got $7400. Boris got $6200. Drusilla got $5400. Erving got $7600. Case 2: Dick got the artificial_arm and paid $190. Tom got $120. Harold got $70. 2009 Westminster High School Programming Contest Problem E: 6 Stringing You Along You are in charge of the Cryptographic Division of the Top Secret organization ... (well, we can't tell you the name since it's Top Secret). One of your jobs is to decode encrypted messages intercepted from the enemy. One way to do this is to look for repeated patterns of characters, which may indicate a common word. For example, in the string sasdflweivhakjsdfhdflwelaj there is a repeated substring dflwe starting at locations 3 and 18. You are to write a program to help in this type of analysis. Input The input le will consist of multiple test cases. The le will start with a single integer n indicating the number of test cases. Each test case will consist of a two lines: the rst line will contain the intercepted message (a string value with no blanks). The second line will contain two integers: i j, indicating the two starting positions to start look for a match. Output For each test case, output the length of the longest matching substrings starting at i and j. If there is no match, you should output 0. Sample Input 3 sasdflweivhakjsdfhdflwelaj 3 18 sasdflweivhakjsdfhdflwelaj 0 2 sasdflweivhakjsdfhdflwelaj 0 1 Sample Output 5 1 0 2009 Westminster High School Programming Contest Problem F: 7 Yahtzee Anna and Tessa have a handheld Yahtzee game, and have devised an interesting method to hold a tournament. They play an even number of games, alternating turns. Every time a player gets a better score than the previous game played, that player gets a point. To make things fair, the player who plays the rst game can get a point if their score is better than the last game played. In addition, 2 points are awarded to the player with the highest score and 1 point is subtracted for the lowest score (if Anna and Tessa tie for highest score, no one gets the 2 point bonus; likewise for the lowest score). For example, suppose Anna and Tessa decide to play six games. Here's how it might go, assuming Anna goes rst: Turn Anna Tessa Anna Tessa Anna Tessa Score 245 294 268 168 219 220 Points Awarded Tessa gets 1 point Anna gets 1 point Tessa gets 1 point; Anna gets 1 point (since her rst score > 220) In addition, Tessa gets 2 points for highest score (294), but 1 point subtracted for lowest score (168) as well. The nal score is Anna 2, Tessa 3, so Tessa wins this tournament. You job will be to write a program which determines the winner when given Anna and Tessa's scores as input. Input The input le will consist of multiple test cases, each test case consisting of one line. Each line will start with an integer n indicating the number of games and will be followed by n positive integers representing the game scores. n will always be even and can be very large (Anna and Tessa have a lot of free time). A line consisting of a single 0 indicates end-of-input. Output For each test case output one of the following three sentences Player 1 wins s1 to s2. Player 2 wins s2 to s1. Both players score s1. where s1 is the rst player's score and s2 is the second player's score. Sample Input 6 245 294 268 168 219 220 8 195 160 296 161 209 256 121 121 0 Sample Output Player 2 wins 3 to 2. Player 1 wins 5 to 1.