Problem H - Palindromia

advertisement
Problem H - Palindromia
Input file: H.IN
Output file: H.OUT
Time limit: 1 sec.
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from
right to left. You are to write a program which, given a string S, determines the number of maximal
palindromes that can be obtained from S by removing some characters.
The palindrome P is maximal if there are no palindromes with length greater then the length P.
Input:
The first line contains one string S. The
(1 <= length(S) <= 100).
string is formed from lowercase letters from ‘a’ to ‘z’
Output:
The first line contains one integer, which is the number of maximal palindromes.
Examples:
Input #1
abca
Output #1
2
Comment:
The maximal palindromes are ‘aba’ and ‘aca’.
Input #2
aaabbbcccddd
Output #2
4
Input #3
abcdabcdabcd
Output #3
36
Problem K: Milk Team Select
Input file: K.IN
Output file: K.OUT
Time limit: 2 sec.
Farmer John's N (1 <= N <= 500) cows are trying to select the milking team for the world-famous Multistate
Milking Match-up (MMM) competition. As you probably know, any team that produces at least X (1 <= X <=
1,000,000) gallons of milk is a winner.
Each cow has the potential of contributing between -10,000 and 10,000 gallons of milk. (Sadly, some cows have a
tendency to knock over jugs containing milk produced by other cows.)
The MMM prides itself on promoting family values. FJ's cows have no doubt that they can produce X gallons of
milk and win the contest, but to support the contest's spirit, they want to send a team with as many parent-child
relationships as possible (while still producing at least X gallons of milk). Not surprisingly, all the cows on FJ's farm
are female.
Given the family tree of FJ's cows and the amount of milk that each would contribute, compute the maximum
number of parent-child relationships that can exist in a winning team. Note that a set of cows with a grandmothermother-daughter combination has two parent-child relationships (grandmother-mother, mother-daughter).
Input:
* Line 1: Two space-separated integers, N and X.
* Lines 2..N+1: Line i+1 contains two space-separated integers describing cow i. The first integer is the number of gallons of
milk cow i would contribute. The second integer (range 1..N) is the index of the cow's mother. If the cow's mother is unknown,
the second number is 0. The family information has no cycles: no cow is her own mother, grandmother, etc.
Output:
* Line 1: The maximum number of parent-child relationships possible on a winning team. Print -1 if no team can win.
Sample Input:
5 8
-1 0
3 1
5 1
-3 3
2 0
Sample Output:
2
Input Details:
There are 5 cows. Cow 1 can produce -1 gallons and has two daughters, cow 2 and 3, who can produce 3 and 5 gallons,
respectively. Cow 3 has a daughter (cow 4) who can produce -3 gallons. Then there's cow 5, who can produce 2 gallons.
Output Details:
The best team consists of cows 1, 2, 3, and 5. Together they produce (-1)+3+5+2 = 9 >= 8 gallons and have 2
parent-child relationships (1--2 and 1--3). Note that a team with cows 2, 3, and 5 would be able to produce more milk (10
gallons), but would have fewer parent-child relationships (0).
Problem L: Shortcut
Input file: L.IN
Output file: L.OUT
Time limit: 2 sec.
Mirek has a favourite way from home to the university that he
traverses every working day. The route consists of sections and
each section is a straight segment 10 meters long. Each section is
either a straight ahead extension of the previous section or it is
perpendicular to the previous section. After traversing each
section Mirek takes a small break to admire the beauty of the
nature. During his walk he never visits the same place twice.
Yesterday Mirek stayed up long in the night at the party and
today he got up late from bed. He knows that he will miss the first
lecture unless he changes his usual route. He plans to make one
shortcut but he wants the shortcut to be as short as possible (well,
we can tell you in secret that he doesn't want to be on time, he
just wants to calm his conscience). The shortcut must be either a
horizontal or vertical segment connecting two break points of Mirek's route.
Please help Mirek find the shortest shortcut.
Write a program that:
• reads Mirek's route,
• computes the shortest shortcut on the route,
• writes the result.
Input:
The first line of the input contains one integer n (3 <= n <= 250 000) being the number of sections of the
route. The second line of the input contains a sequence of n characters N, E, S or W with no spaces in between.
Each character is a description of one section of the route. Character N, E, S or W means that Mirek walks 10
meters north, east, south or west respectively. You may assume that at least one shortcut exists for the given route.
Output:
The first and only line of the output contains integers I, b, e and character d separated by single spaces. Integer I
is the length of the shortest shortcut (measured in 10 m segments). Integers b and e axe the numbers of break points
where the shortcut begins and ends respectively (we number break points with consecutive integers from 0 for
Mirek's home to n for the university). Character d is the direction of the shortcut. If more than one shortcut of the
minimal length exists you should output the one that begins earliest on the route. If more than one shortcut of the
minimal length begins at the same break point you should output the one that ends furthest on the route.
Sample Input:
Sample Output:
12
NNNENNWWWSSW
2 3 11 W
Problem M: Picture
Input file: M.IN
Output file: M.OUT
Time limit: 2 sec.
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are
all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of
the union of all rectangles is called the perimeter.
Write a program to calculate the perimeter.
An example with 7 rectangles is shown in Figure l.
The corresponding boundary is the whole set of line segments drawn in Figure 2.
Figure 1. A set of 7 rectangles
Figure 2. The boundary of the set of rectangles
The vertices of all rectangles have integer coordinates.
Input:
The first line of the file contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can
find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those
coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.
Output:
The file must contain a single line with a non-negative integer which corresponds to the perimeter for the input
rectangles.
Constraints:
0 < number of rectangles < 5000. All coordinates are in the range [-10000,10000] and any existing rectangle has a
positive area. The numeric value of the result may need a 32-bit signed representation.
Sample Input:
7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
This corresponds to the example of Figure 1.
Sample Output:
228
This is the contents of the output file for the example
above.
Download