0% found this document useful (0 votes)
0 views

coding king question

The document outlines several programming tasks, including finding occurrences of a pattern in a string, solving the 15 puzzle, determining the level of diversity in nice ab-words, and calculating the minimal shifts needed to move a parcel in a store. Each task includes specific input and output requirements, along with examples to illustrate the expected results. The document emphasizes the need for efficient algorithms and handling of multiple test cases.

Uploaded by

mredward eu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

coding king question

The document outlines several programming tasks, including finding occurrences of a pattern in a string, solving the 15 puzzle, determining the level of diversity in nice ab-words, and calculating the minimal shifts needed to move a parcel in a store. Each task includes specific input and output requirements, along with examples to illustrate the expected results. The document emphasizes the need for efficient algorithms and handling of multiple test cases.

Uploaded by

mredward eu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

A Needle in the Haystack

Write a program that finds all occurences of a given pattern in a given input string. This is often
referred to as finding a needle in a haystack.

The program has to detect all occurences of the needle in the haystack. It should take the needle
and the haystack as input, and output the positions of each occurence, as shown below. The
suggested implementation is the KMP algorithm, but this is not a requirement. However, a naive
approach will probably exceed the time limit, whereas other algorithms are more complicated... The
choice is yours.

Input
The input consists of a number of test cases. Each test case is composed of three lines, containing:

● the length of the needle,


● the needle itself,
● the haystack.

The length of the needle is only limited by the memory available to your program, so do not make
any assumptions - instead, read the length and allocate memory as needed. The haystack is not
limited in size, which implies that your program should not read the whole haystack at once. The
KMP algorithm is stream-based, i.e. it processes the haystack character by character, so this is not a
problem.

The test cases come one after another, each occupying three lines, with no additional space or line
breaks in between.

Output
For each test case your program should output all positions of the needle's occurences within the
haystack. If a match is found, the output should contain the position of the first character of the
match. Characters in the haystack are numbered starting with zero.

For a given test case, the positions output should be sorted in ascending order, and each of these
should be printed in a separate line. For two different test cases, the positions should be separated
by an empty line.

Example
Sample input:
2
na
banananobano
6
foobar
foo
9
foobarfoo
barfoobarfoobarfoobarfoobarfoo

Sample output:
2
4

3
9
15
21

Solving the Puzzle


The 15 puzzle is a classic puzzle made famous in the 19th century. It consists of 4x4 board with 15
sliding tiles numbered from 1 to 15. The objective is to get them into this pattern:

1 2 3 4

5 6 7 8
9 10 11 12
13 14 15

Here we will deal with a generalized version of the above puzzle. You should write a program that
given some initial state of the nxn board finds a sequence of moves that transforms it so that in the
i-th row there are tiles with numbers i*n+1,i*n+2,...,i*n+n (from left to right) - with the exception of the
lower right corner where the hole should be. The less moves you use, the more points you get.

Input
The first line of input contains the number of test cases c (c<=200). Then c test cases follow, each of
them begins with a line with a single integer n (3<=n<=10) in it. The next n lines describe the initial
state of the board - the i-th line consists of exactly n integers describing the i-th row. The position of
the hole is indicated by 0.

Output
For each test case output one line - the found sequence of moves. Write 'D' to move the hole down,
'U' to move it up, 'R' to move it right and 'L' to move it left. You shouldn't use more than 10000
moves. All moves should be valid (so for example don't try to move the hole up when it is in the first
row).

Scoring
Your program will receive n^3/(m+1) points for each test case where m is the number of moves.

Example
Input:

1 2 7 3

5 6 0 4

9 10 11 8

13 14 15 12

0 1 2

4 5 3

7 8 6

Output:

URDDD

RRDD
AB-word
s
Every sequence of small letters a and b (also the empty sequence) is called an ab-word. If X = [x1,
..., xn] is an ab-word and i, j are integers such that 1 <= i <= j <= n then X[i..j] denotes the subword of
X consisting of the letters xi, ..., xj. We say that an ab-word X = [x1..xn] is nice if it has as many letters
a as b and for all i = 1, ..., n the subword X[1..i] has at least as many letters a as b.

Now, we give the inductive definition of the similarity between nice ab-words.

● Every two empty ab-words (i.e. words with no letters) are similar
● Two non-empty nice ab-words X = [x1, ..., xn] and Y = [y1, ..., ym] are similar if they have the
same length (n = m) and one of the following conditions if fulfilled:
1. x1 = y1, xn = yn and X[2..n-1] and Y[2..n-1] are similar ab-words and they are both
nice;
2. there exists i, 1 <= i <= n, such that X[1..i], X[i+1..n] are nice ab-words and
1. Y[1..i], Y[i+1..n] are nice ab-words and X[1..i] is similar to Y[1..i] and X[i+1..n]
is similar to Y[i+1..n], or
2. Y[1..n-i], Y[n-i+1..n] are nice ab-words and X[1..i] is similar to Y[n-i+1..n] and
X[i+1..n] is similar to Y[1..n-i].

A level of diversity of a non-empty set S of nice ab-words is the maximal number of ab-words that
can be chosen from S in such a way that for each pair w1,w2 of chosen words, w1 is not similar to
w2.

Task
Write a program that for each test case:

● reads elements of S from standard input;


● computes the level of diversity of the set S;
● writes the result to standard output.

Input
The number of test cases t is in the first line of input, then t test cases follow separated by an empty
line.
In the first line of a test case there is a number n of elements of the set S, 1 <= n <= 1000; in the
following n lines there are elements of the set S, i.e. nice ab-words (one word in each line); the first
letter of every ab-word is the first symbol in line and there are no spaces between two consecutive
letters in the word; the length of every ab-word is an integer from the range [1..200].

Output
For each test case your program should output one line with one integer - the level of diversity of S.

Example
Sample input:

aabaabbbab

abababaabb

abaaabbabb

Sample output:

Store-keepr
The floor of a store is a rectangle divided into n*m square fields. Two fields are adjacent, if they have
a common side. A parcel lays on one of the fields. Each of the remaining fields is either empty, or
occupied by a case, which is too heavy to be moved by a store-keeper. The store-keeper has to shift
the parcel from the starting field P to the final field K. He can move on the empty fields, going from
the field on which he stands to a chosen adjacent field. When the store-keeper stays on a field
adjacent to the one with the parcel he may push the parcel so that if moves to the next field (i.e. the
field on the other side of the parcel), assuming condition that there are no cases on this field.
Task
Write a program, which:

● reads from the standard input a store scheme, a starting position of the store-keeper and a
final position of the parcel,
● computes minimal number of the parcel shifts through borders of fields, which are necessary
to put the parcel in the final position or decides that it is impossible to put the parcel there,
● writes the result into the standard output.

Input
The number of test cases t is in the first line of input, then t test cases follow separated by an empty
line. In the first line of each test case two positive integers separated by a single space, n,m<=100,
are written. These are dimensions of the store. In each of the following n lines there appears one
m-letter word made of letters S, M, P, K, w. A letter on i-th position in j-th word denotes a type of the
field with coordinates (i,j) and its meaning is following:

● S - case,
● M - starting position of the store-keeper,
● P - starting position of the parcel,
● K - final position of the parcel,
● w - empty field.

Each letter M, P and K appears in the test case exactly once.

Output
Your program should write to the standard output for each test case:

● exactly one word NO if the parcel cannot be put on the target field,
● exactly one integer, equal to the minimal number of the parcel shifts through borders of the
fields, necessary to put a parcel on a final position, if it is possible to put the parcel there.

Example
Sample input:

10 12

SSSSSSSSSSSS
SwwwwwwwSSSS

SwSSSSwwSSSS

SwSSSSwwSKSS

SwSSSSwwSwSS

SwwwwwPwwwww

SSSSSSSwSwSw

SSSSSSMwSwww

SSSSSSSSSSSS

SSSSSSSSSSSS

Sample output

Subset-Sum
You are given an array of N integers. Now you want to find the sum of all those integers which can
be expressed as the sum of at least one subset of the given array.

Input
First line contains T the number of test case. then T test cases follow, first line of each test case
contains N (1 <= N <= 100) the number of integers, next line contains N integers, each of them is
between 0 and 1000 (inclusive).

Output
For each test case print the answer in a new line.

Example
Input:
2

0 1

2 3 2

Output:

21

Special Graph
You are given a directed graph with N vertices. The special thing about the graph is that each vertex
has at most one outgoing edge. Your task is to answer the following two types of queries:

● 1 a delete the only edge outgoing from vertex a. It is guaranteed that the edge exists. 1 <= a
<= N.
● 2 a b output the length of the shortest path from vertex a to vertex b, if the path exists.
Otherwise output "-1" without quotes. 1 <= a, b <= N.

Input
First line of input contains a natural number N <= 10^5 the number of vertices in the graph.

The following line contains N integer numbers, i-th number is next[i] (0 <= next[i] <= N), meaning that
there is an edge from vertex i to vertex next[i]. If next[i] = 0, assume that there is no outgoing edge
from vertex i.

Third line contains a natural number M<=10^5 the number of queries.

The following M lines contain a query each. Queries are given in the manner described above.
Output
On the i-th line output the answer for the i-th query of type 2 a b.

Example
Input:

3 3 4 5 6 4

2 1 6

2 1 4

2 1 2

1 3

2 1 6

2 1 4

Output:

-1

-1

-1
Sum of primes
Léo had been defeated by XerK at the battle of the ThermoPell. 300 braves fell ; XerK as a living
God decided to allow a new honor table, for those who can use less than 900 characters in his new
problem. This problem is extremely simple, but you have to be extremely fast.

Input
The lonely line of input contains an integer N.

Output
You have to print the sum of all primes up to N.

Examples
Input_1:

19

Output_1:

77

Input_2:

1000000000

Output_2:

24739512092254535

Explanation
The first sum is

2 + 3 + 5 + 7 + 11 + 13 + 17 + 19 = 77
Constraints
0 < N <= 2×10^9

The classical problem SUMPRIM2 is the reverse task.


Time limit allows some slow languages to finish in time, it could be hard.
For your information, my 690-Byte C code need a total time of 0.76s for the 30 input files, 22.82s for
my Python one. (Edit 12/II/2017, after compiler update).
Warning : You have 900B as code size limit.
;-) Have fun.

Ada and Jobs


As you might already know, Ada the Ladybug has a huge TODO-list. Sometimes she inserts a new
job into her TODO-list and sometimes she is wondering whether there is a job (in her TODO-list),
which she wants to do now. She doesn't require the whole job to be there, perhaps just a part of it.

Can you create a program which would serve her? That means it either inserts a new job into her
TODO-list or answers whether there exists a word (in her TODO-list) which is a substring of given
word.

Input
The first line of input contains Q, the number of queries.

The next Q lines contains a number 0 ≤ t ≤ 1 and nonempty string s. If t is equal to 0 then it is
insertion query, otherwise it is question query. s consists of lowercase characters only.

The sum of lengths of queries of both types doesn't exceed 106 (that means the total sum of lengths
of strings will be at most 2*106)

Output
For each query of type 1, print either YES, if there is already a substring in the TODO-list and NO
otherwise.

Example Input
12

0 cat

1 dogville

0 dog

1 dogville

1 gooutwithcat

1 gooutwithcrocodile

1 fancyconcatenation

0 crocodile

1 lacoste

1 gooutwithcrocodile

1 catalanreferendum

1 rocodile

Example Output
NO

YES

YES

NO

YES

NO

YES

YES

NO
Stopping-off
Cities
You work for a tour operator which plans to commercialize different visiting tours in a country. A tour
is a sequence of cities that are visited during the trip. A city cannot be visited more than once in one
tour. Each city is represented as a node in a non-oriented graph where edges are possible
connections. Given a departure city A and a destination city B, we call stopping-off-city a city that is
part of at least one possible tour between A and B. Your mission is to select all possible
stopping-off-cities between A and B. In the example of the figure bellow, we have a graph of 20
cities. If we consider the departure as node 10 and the arrival as node 16 the stopping-off-cities are
{8, 9, 10, 11, 12, 13, 15, 16}.

Input
First line of input consists of an integer V denoting the number of nodes or cities (V<=10000). Then,
each line contains an edge definition as two space separated integers (link between two cities).
Edges description ends with the line "-1 -1" (without cotes). The last line contains two space
separated integers "s d" where s is the departure city and d the destination city.

Output
A space separated sorted list of stopping-off-cities including s and d. It is guaranteed that at least
one path exists between s and d.

Example
Input:

20

0 1

1 2

2 3

3 4

4 5

5 6

6 7

1 8

8 11

8 9

9 10

11 12

9 12

12 13

12 15

12 14

14 19

14 18
14 17

17 18

18 19

13 15

15 16

-1 -1

10 16

Output:

8 9 10 11 12 13 15 16

Minimum string moves


We have two strings A and B which are permutations of the same set of characters. We need to
change these strings to obtain two identical strings by performing the following operations:

1. swap two consecutive characters of a string


2. swap the first and the last characters of a string

The operation can be performed on either string. Return the minimum number of moves that we
need in order to obtain two equal strings?

Constraints
1 < length(A) = length(B) ≤ 2,000

All the input characters are between 'a' and 'z'

The count of each distinct character in A is identical to the count of the same character in B.

Input
First line: String A.
Second line: String B.

Output
Minimum number of moves.

Example
Input:

aab

baa

Output:

You might also like