Sei sulla pagina 1di 9

Problem : Retrieve Magical Substring

Given a string S, and two integers L, P, find the substring of length L that occur P times in the string S. For
example, if S = pujanshahpujan, N = 5, P = 2, we need substrings of length 5 that occur twice in S. Clearly
this is pujan.

Input Format:

First line contains string S

Second line contains T, the number of test cases

The next T lines each contain a pair of integers L and P

Output Format:

For each test case,

K lines each containing a substring of S with length L that occurs P times in S

Constraints:

Length of S 1000
T 50

Example 1

Input
abacaba
1
32

Output
aba

Explanation
We need substrings of length 3 occurring twice in S. Clearly, aba is the only such substring

Example 2

Input
mynameispujanshahandmybrothersnameischintanshah
1
42

Output
name
amei
meis
ansh
nsha
shah
Explanation
We need substrings of length 4 occurring twice.

Problem : Tug of War

TCS organizes a party for all Philacodistclub members who contributed questions to the CodeVita contest. As
part of the recreation activity, the members decide to play TUG OF WAR. The members are to be divided
into two teams "A" and "B" in a such a way that all the members in each team know each other. The teams
must have non zero sizes but need not have the same number of members. Also, each member belongs to
one of the teams.

Write a program to determine whether such teams could be formed.

Input Format:

First line contains N, the number of members in the club

Second line contains M the number of mutual friendships

The next M lines each contain a pair of comma separated integers p, q meaning member p knows member q

Output Format:

YES if it is possible to form two teams with conditions stated above, NO if no teams could be formed

Constraints:

N 200
M 100

Example 1

4
3
12
13
14

Output
NO

Explanation
Since 2, 3 and 4 do not know each other, no two of them can be present in the same team. But among the
three two must be placed in a team and hence no team formation is possible.

Example 2

Input
4
3
12
23
34

Output
YES

Explanation
1 and 2 can be in a team and 3 and 4 in the other team.

Problem : Aligning Catapults

Caesar wishes to lay siege to the indomitable Gauls, and moves his catapults (hurling huge rocks) to all
point in a line to the Gaulish village. The warriors of Gaul, Asterix and Obelix, are very good at
counterattack, and can be stopped only if the catapults are all in line towards the Gaulish village (towards
North). Also, the catapults are so heavy that he needs many men and horses to move one, and he can move
only one catapult at a time. Also he knows that the counterattack is coming soon, and he needs to align
them quickly.

For simplicity, we model the situation as a n x n grid of squares with the initial position of the n catapults
given. We need to advise Caesar on the minimum number of moves to align them in a vertical (northward)
direction. A catapult can move North-South (up-down) or East West (left-right) but not diagonally.

Input Format:

The first line consists of one positive integer n

There are then a set of n lines, each corresponding to an East West row in the grid, consisting of a set of
comma separated numbers (1 or 0) representing the presence or absence of a catapult in that position of
the grid in that row.

The total number of 1's across all the rows will be n

Output Format:

One line containg a non-negative integer indicationg the number of moves necessary to align the catapults
in a vertical direction.
Constraints:

5n50

Example 1

Input
5
0,1,0,0,0
0,0,0,1,0
0,0,0,1,0
0,0,0,0,0
1,0,1,0,0

Output
6

Explanation
One set of minimal moves required are (1,2)-(1,3), (2,4)-(2,3), (3,4)-(3,3), (5,1)-(4,1)-(4,2)-(4,3)

Example 2

Input
5
1,1,0,0,0
0,0,0,0,0
0,1,0,0,0
0,1,0,0,0
0,0,1,0,0

Output
3

Explanation
One set of moves is (1,1)-(2,1)-(2,2), (5,3)-(5,2)

The prime numbers are written in a spiral form staring at (0,0) and moving as shown in the diagram below.
The numbers shown on the right column and the bottom row are the column numbers and row numbers
respectively (y and x coordinate frames).
The objective is to find the position (x and y coordinates) of a given prime.

Input Format:

The input consists of multiple lines.


The first line gives the number of primes (N) in this test case.
The next N lines contain one prime in each line.

Output Format:

The output consists of N lines.


Each consists of a space separated pair of integers giving the x and y coordinates of the corresponding
prime in the input.

Constraints:

N10
Each prime < 1000000

Example 1
Input
2
3
7

Output
10
01

Explanation
There are 2 primes in this test case (N=2). The primes are 3 and 7. The coordinates of these in the spiral is
(1,0) and (0,1). The output hence has these in space separated form.

Example 2
Input
3
5
11
13

Output
11
-1 1
-1 0

Explanation
There are 3 primes in this test case (N=2). The primes are 5, 11 and 13. The coordinates of these in the
spiral is (1,1), (-1,1) and (-1,0). The output hence has these in space separated form.

Problem : Sorting Boxes

The parcel section of the Head Post Office is in a mess. The parcels that need to be loaded to the vans have
been lined up in a row in an arbitrary order of weights. The Head Post Master wants them to be sorted in the
increasing order of the weights of the parcels, with one exception. He wants the heaviest (and presumably
the most valuable) parcel kept nearest his office.

You and your friend try to sort these boxes and you decide to sort them by interchanging two boxes at a
time. Such an interchange needs effort equals to the product of the weights of the two boxes.

The objective is to reposition the boxes as required with minimum effort.

Input Format:

The first line consists of two space separated positive integers giving the number of boxes (N) and the
position of the Head Post Master's office (k) where the heaviest box must be.

The second line consists of N space separated positive integers giving the weights of the boxes. You may
assume that no two weights are equal.

Output Format:

The output is one line giving the total effort taken to get the boxes in sorted order, and the heaviest in
position k.

Constraints:

N50
Weights 1000

Example 1

Input
52
20 50 30 80 70

Output
3600

Explanation
There are 5 boxes (N=5) and the heaviest box must be in position 2 (k=2). If we look at the final order
(sorted, with the heaviest at position 2), it should be 20 80 30 50 70. If we look at this, we notice that only
the 50 and the 80 parcels need to be exchanged. As this takes effort of the product of the weights, the effort
is 4000.

Further reduction can be obtained if we use the smallest package (20) as an intermediary. If we exchange
20 with 50 (effort 1000), then with 80 (effort 1600) and back with 50 again (effort 1000), the effect is the
same, with a total effort of 3600 (less than the effort obtained by the direct move) an the effort

The results after the optimal sequence of exchanges are

50 20 30 80 70
50 80 30 20 70
20 80 30 50 70

As this takes an effort of 3600, the output is 3600.

Example 2

Input
63
30 20 40 80 70 60

Output
7600

Explanation
There are 6 parcels, and the heaviest should be at position 3. Hence the final order needs to be 20 30 80 40
60 70. If we look at the initial position, we see that 20 and 30 need to be exchanged (effort 600), 40 and 80
need to be exchanged (effort 3200) and 60 and 70 need to be exchanged (effort 4200). Hence the total
effort is 600+3200+4200=8000.

If we use the same approach as in Example 1, we get the following efforts

(600) 20 30 40 80 70 60
(3200) 20 30 80 40 70 60
(1200) 60 30 80 40 70 20
(1400) 60 30 80 40 20 70
(1200) 20 30 80 40 60 70

A total effort of 7600 is obtained rather than an effort of 8000, which is the output.

Problem : Solitaire

A deck of alphabet cards contains cards marked with letters of the alphabet (A,B,C,..). Each letter appears
on one or more cards. The pack is shuffled and the cards dealt out one by one into a number of stacks. Any
number of stacks may be used, but after all the cards have been dealt, you must get all the cards into
alphabetical order, with all cards showing A first and then all cards showing B and so on. Since they are
stacked, only the top card of any stack can be retrieved.

Given the order of the cards, the objective is to determine the minimum number of stacks which enables the
final operation of getting them into alphabetic order.
For example, if the order of the cards is ABNJJBBA, the stacks after dealing could be

AA, BBB, NJJ

It is obvious that this can be achieved during the dealing process. During the retrieval process, all the A's
can be picked up from stack 1 one at a time, by accessing only the top of the stack, all the B's can be
obtained in the same way, and then the J's. Once the J's have been picked up, the N's are on top of the
stack and can be picked up.

Input Format:

One line containing an integer N that defines how many sequences appear in this test case.

After the first line, N lines, each containing a sequence of space separated upper case letters. Note that not
all letters need to be present, and that a letter may appear many times

Output Format:

N lines, each containing one integer representing the number of stacks needed for the corresponding
sequence of cards in the input

Constraints:

1N20
Numbers of cards in each sequence 100

Example 1

Input
2
GKLCAJEI
GLJBNJFAMB

Output
3
3

Explanation
There are two sequences (N=2). For the first sequence, one possible set of stacks is GCA, KI, LJE.
With these 3 stacks, it is possible to get the cards in alphabetic order. Similarly for the second sequence, a
possible set of stacks is GGB, NM, JJA,LF. Hence the output is 3 for the first sequence and 3 for the second
sequence.

Example 2

Input
2
EGDEBHCGHD
GCDEBHDNCJ

Output
4
5

Explanation
There are two sequences (N=2). For the first sequence, one possible set of stacks is EEB, GDC, HH, GD. For
the second, possible set of stacks is GC, DDC, EB, H, NJ. As there are 4 and 5 stacks respectively, the
output is 4 and 5.

Potrebbero piacerti anche