Problem 1: Rock, Scissors, Paper

advertisement
Programming Contest
@
MTSU Computer Science Department
Date:
March 20, 2009
Notes:
1. You need to create sample input files by yourself
to test your program. The name of the sample
input file must be the same as the one given in
each problem.
2. Input files and your source codes should be in
the same folder.
3. All output should be directed to the screen.
Problem 1: Mirror, Mirror
Input File: mirror.in
Walking towards his parked car, John was amused to note what appeared to be a string of gibberish painted
along the front of an emergency vehicle parked behind him: ECNALUBMA.
It was only after he climbed into his own car and glanced into his rear-view mirror that the string suddenly
made sense: AMBULANCE.
John belatedly realized that even the individual letters had been reversed, but that he had mentally corrected
these without even being aware of doing so. It was the rearrangement of the letters into an appropriate word that
had stymied him.
Fascinated with this idea, John would now like to practice his mirror-reading skills. Write a program that
accepts input phrases and prints them, reversing the order of the characters.
Input
Input consists of one or more lines, each containing from 1 to 80 printable ASCII characters (alphabetics,
numerals, blanks, and punctuation).
End of input is signaled by a line containing only the string “***”.
Output
For each line of input data, your program should print a single line of output containing the same characters as
the input line, in reverse order.
Example Input:
AMBULANCE
Evian
madam, i’m adam
***
Example Output:
ECNALUBMA
naivE
mada m’i ,madam
Problem 2: Take Your Vitamins
Input File: vitamins.in
Manufacturers of food products are required to place nutrition information labels on their packages. A major
part of this information is a listing of important vitamins and minerals, listing both the amount of the chemical
present in one serving and the percentage of an adult’s minimum daily requirement for that chemical.
Write a program to help prepare these nutritional labels by computing that percentage from the information on
the amount present in one serving and the amount constituting the minimum daily requirement.
Input
Input consists of one or more lines, each of the form:
AURV
where A is the amount of a vitamin/mineral present in one serving of the food product, U is the units in which A
is measured, R is the minimum daily requirement for that vitamin/mineral, measured in the same units as A, and
V is the name of that vitamin/mineral.
A and R will be floating point numbers. U will be a string of alphabetic characters with no embedded spaces. V
will be a string of characters, possibly including spaces. A, U, R, and V will be separated from one another by
exactly one space, and V is terminated by the end of the input line.
End of the input is signaled by a line in which A is negative.
Output
For each line of input data, your program should determine the percentage of the recommended daily
requirement being provided for that vitamin/mineral. If it is at least 1%, your program should print a line of the
form
V A U P%
where V, A, and U are the quantities from the input, and P is the percentage of the minimum daily requirement
represented by the amount A.
V should be printed left-justified on the line. A should be printed with 1 digit precision, and P with zero digits
precision. V, A, U, and P should be separated by one space each.
After the last such line, your program should print a line stating
Provides no significant amount of:
followed by a list of the names of all vitamins/minerals which are provided as less than 1% of the minimum
daily requirement. These should be printed one name per line, in the order they occurred within the input.
Example Input:
3500.0 iu 5000.0 Vitamin A
60.0 mg 60.0 Vitamin C
0.15 g 25.0 Fiber
109. mg 990. Phosphorus
0.0 mg 1000.0 Calcium
25.0 mg 20.0 Niacin
-1.0 x 0.0 x
Example Output:
Vitamin A 3500.0 iu 70%
Vitamin C 60.0 mg 100%
Phosphorus 109.0 mg 11%
Niacin 25.0 mg 125%
Provides no significant amount of:
Fiber
Calcium
Problem 3: Semi-prime H-numbers
Input File: Hnumbers.in
This problem is based on an exercise of D. Hilbert, who pedagogically suggested that one study the theory of
4n+1numbers. Here, we do only a bit of that.
An H-number is a positive number which is one more than a multiple of four: 1, 5, 9, 13, 17, 21,... are the Hnumbers. For this problem we pretend that these are the only numbers. The H-numbers are closed under
multiplication.
As with regular integers, we partition the H-numbers into units, H-primes, and H-composites. 1 is the only unit.
An H-number h is H-prime if it is not the unit, and is the product of two H-numbers in only one way: 1 × h.
The rest of the numbers are H-composite.
For examples, the first few H-composites are:
5 × 5 = 25, 5 × 9 = 45, 5 × 13 = 65, 9 × 9 = 81, 5 × 17 = 85.
Your task is to count the number of H-semi-primes. An H-semi-prime is an H-number which is the product of
exactly two H-primes. The two H-primes may be equal or different. In the example above, all five numbers are
H-semi-primes. 125 = 5 × 5 × 5 is not an H-semi-prime, because it's the product of three H-primes.
Input
Each line of input contains an H-number ≤ 1,000,001. The last line of input contains 0 and this line should not
be processed.
Output
For each inputted H-number h, print a line stating h and the number of H-semi-primes between 1 and h
inclusive, separated by one space in the format shown in the sample.
Example input
21
85
789
0
Example output
21 0
85 5
789 62
Problem 4: Fun Projects
Input File: fun.in
At an open-source fair held at a major university (UCSD of course), leaders of open-source projects put sign-up
sheets on the wall, with the project name at the top in capital letters for identification.
Students then signed up for projects using their userids. A userid is a string of lower-case letters and numbers
starting with a letter.
The organizer then took all the sheets off the wall and typed in the information.
Your job is to summarize the number of students who have signed up for each project. Some students were
overly enthusiastic and put their name down several times for the same project. That's okay, but they should
only count once. Students were asked to commit to a single project, so any student who has signed up for more
than one project should not count for any project.
There are at most 10,000 students at the university, and at most 100 projects were advertised.
Input
The input contains several test cases, each one ending with a line that starts with the digit 1. The last test case is
followed by a line starting with the digit 0.
Each test case consists of one or more project sheets. A project sheet consists of a line containing the project
name in capital letters, followed by the userids of students, one per line.
Output
For each test case, output a summary of each project sheet. The summary is one line with the name of the
project followed by the number of students who signed up. These lines should be printed in decreasing order of
number of signups. If two or more projects have the same number of signups, they should be listed in
alphabetical order.
Example input
Example output
UBQTS TXT
tthumb
LIVESPACE BLOGJAM
philton
aeinstein
YOUBOOK
j97lee
sswxyzy
j97lee
aeinstein
SKINUX
1
0
YOUBOOK 2
LIVESPACE BLOGJAM 1
UBQTS TXT 1
SKINUX 0
Problem 5: Simpsons’ Hidden Talents
Input File: talent.in
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix in
Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
Input
Input consists of multiple test cases followed by a special line with two “end” strings indicating the end of all
test cases.
A test case occupies one line, which contains two strings – representing s1 and s2 respectively. There is no
space character within s1 or s2.
The lengths of s1 and s2 will be at most 50000. You may assume all letters are in lowercase.
Output
Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed
by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
Example Input:
clinton homer
riemann Marjorie
end end
Example Output:
0
rie 3
Problem 6: Rock, Scissors, Paper
Input File: rsp.in
Bart’s sister Lisa has created a new civilization on a two-dimensional grid. At the outset each grid location may
be occupied by one of three life forms: Rocks, Scissors, or Papers.
Each day, differing life forms occupying horizontally or vertically adjacent grid locations wage war. In each
war, Rocks always defeat Scissors, Scissors always defeat Papers, and
Papers always defeat Rocks. At the end of the day, the victor expands its territory to include the loser’s grid
position. The loser vacates the position.
Input
Your job is to determine the territory occupied by each life form after n days. The first line of input contains t,
the number of test cases. Each test case begins with three integers not greater than 100: r and c, the number of
rows and columns in the grid, and n. The grid is represented by the r lines that follow, each with c characters.
Each character in the grid is R, S, or P, indicating that it is occupied by Rocks, Scissors, or Papers respectively.
Output
For each test case, print the grid as it appears at the end of the nth day. Leave an empty line between the output
for successive test cases.
Example input
2
331
RRR
RSR
RRR
342
RSPR
SPRS
PRSP
Example output
RRR
RRR
RRR
RRRS
RRSP
RSPR
Download