Você está na página 1de 3

PROBLEM 1

STRINGS
input : standard input
output : standard output
Suppose your friend who is far away has to send you a message using an
unreliable communication channel. The message to be sent is a character
string of n characters. Each character in the message string is a lowercase
alphabet from 'a' to 'z'. Unfortunately, errors may be introduced in the
message during transmission. The only types of errors possible are
replacement of one character by another lowercase alphabetic character.
Hoping to get the message across, your friend sends the same message several
times. Each time some errors may occur and you receive some character string
as the message. Your task is to try to figure out the actual message from
all the character strings received. You assume that too many errors do not
occur and many characters would be received correctly.
>If s1 and s2 are two character strings with n characters each, define
>distance(s1,s2) = number of positions where the two strings differ. If the
>message was sent k times and s1, s2, ..., sk were the strings received, we
>will assume that the actual message was a string m such that the total
>distance = distance(m,s1) + distance(m,s2) + ... + distance(m,sk) is
>minimum. There may be more than one such string m for which the total
>distance is minimized. You have to write a program which takes k strings of
>length n each as input and outputs the minimum total distance and the
>number of different strings m for which the minimum occurs.

Input
The input will consist of several test cases. Each test case will be
identified by a unique number written on the first line. If this number is 0
it indicates the end of input.
The next line will contain two integers n and k separated by a single space.
The next k lines will contain the strings s1, s2, ...., sk, one on each
line, each containing exactly n lowercase alphabetic characters. There will
be no line separating consecutive test cases. There will be no leading
spaces on any line.

Output
The output should consist of the test case number (nonzero) on the first
line. The second line should contain two integers separated by a single
space. The first integer should be the minimum total distance and the second
integer should be the number of different strings for which the total
distance is minimized.

Sample Input
1
34
eat
bat
bar
car
2
51
hello
0

Sample Output
1
42
2
01
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
PROBLEM 2
SWEETS
input : standard input
output : standard output
Ramu runs a sweet shop exclusively for children. He stocks many varieties of
sweets (no two varieties cost the same, all sweets in any one variety are
identical in weight and cost, and costlier sweets also weigh more than
cheaper ones). Parents send children to the shop with some amount of money.
Bewildered by the great variety of sweets, the children simply give all the
money to Ramu and ask him to give any combination of sweets that can be
bought exactly for that total amount.
>
>Ramu uses the following algorithm to decide sweet selection- if no
>combination costs the exact amount of money with the child, he sends him
>back with no sweets and asks him to come back with his parents. Otherwise,
>if the child is less than 3 years old, he gives the combination of sweets
>whose total weight is the least. If the child is 3 or older, he gives the
>combination whose total weight is the maximum.

INPUT
The input will contain several test cases.
Any line beginning with semi-colon is a comment to be ignored.
Each test case is indentified by a unique number which will be written on
the first line. If this number is 0 it indicates end of input.
The second line of each test case will contain one positive integer n which
is the number of varieties of sweets.
The next n uncommented lines contain 2 positive integers and (cost and
weight of i-th sweet).
The next uncommented line contains a positive integer m which is the number
of children who come to buy sweets.
The next m uncommented lines will contain 2 positive integers and (age and
amount to spend for each child)

OUTPUT
Output for each test case is to print one line for each child with 0 (if
sent back with no sweets) or two positive integers separated by one space
which are the total number of sweets given to child and total weight of
sweets.

SAMPLE INPUT(lines beginning with ; will not be present in input file)


; input file.
; test case number is first
1
; next is number of varieties of sweets
2
; next is cost and weight of each sweet
37
5 10
; number of children
3
; age and spending amount for each child
1 38
6 38
67
0
SAMPLE OUTPUT
8 77
12 87
0

--------------------------------------------------------------------------------
PROBLEM 3
GRID
input : standard input
output : standard output
A large housing complex contains several bungalows which are arranged in a
square grid containing n bungalows in each row and column. It is Diwali time
and the residents of some of the bungalows have decorated their buildings
with bright lights. Seeing others decorating their bungalows, many of the
residents are under peer pressure to do the same. The rule they follow is
this: If at least two of their neighbouring buildings to the north, south,
east, west have decorated their buildings then they will also do it. Note
that some buildings may not have neighbours in all four directions but they
use the same rule considering only their actual neighbours. Each bungalow
has at least two and at most four neighbours if n 1.
Assuming at a point of time say t=0, a certain number of bungalows(zero or
more) are initially decorated as specified in the input. Your program should
output the number of undecorated buildings at t = infinity.

Input
The input will contain several test cases. Each test case is indentified by
a unique number which will be written on the first line. If this number is 0
it indicates end of input. The second line of each test case will contain
two integers separated by a single space, n the number of bungalows in each
row and column and m the total number of bungalows that are intially
decorated. The third line will contain 2m integers where , and the ith
decorated bungalow is in row and column , for .

Output
The output should contain the test case number (nonzero) on one line
followed by the number of undecorated bungalows for that case on the next
line. There should be no line separating the outputs of different test
cases.

Sample input
1
32
0022
2
43
112023
0

Sample output
1
7
2
8

Você também pode gostar