National High School Programming Contest: Junior Level
National High School Programming Contest: Junior Level
Problem A
Abdullah the Little Explorer
Time Limit: 1 second
Abdullah is a young explorer from Bangladesh. He has traveled to the Never Never Land to explore the
vast deserts there. Equipped with his GPS and radio transmitter, he is travelling through the deserts all
by himself. He keeps regular contact with the Base Camp, so that the Elders are aware of his location
and his safety.
Suddenly, Abdullah finds his GPS is not working! And as luck would have it, his radio transmissions are
also failing. Wandering in the desert, he no longer knows his current position. However, he remembered
his last known position. The position can be represented as a point in a 2 dimensional XY plane, where
the x axis runs from West to East and y axis from South to North. He also kept track of the directions he
travelled and for how many miles. He only traveled along North, South, East and West.
All on a sudden, his radio starts working and he immediately transmits his last known position and the
sequence of moves he made thereafter. Can the people in the Base Camp identify his current location?
Input
The first line of the input contains T, the number of test cases (1 <= T <= 10). The first line of each test
case contains 2 space separated integers x, y, representing Abdullah’s last known position (0 <= x, y <=
1000). Next line contains a string representing his subsequent moves. There can be at least 1 move and
at most 10000 moves. Each move is represented by 2 letters. The first letter is either of N, S, E, W
representing the direction he travelled. The next letter is between 0 and 9. It represents the number of
miles he traveled in that direction. The subsequent test case starts in the next line.
Output
For each test case output 2 space separated integers representing the x and y coordinates of Abdullah’s
current position.
Problem B
Sequence of Numbers
Time Limit: 1 second
An arithmetic sequence is a sequence of numbers such that the difference between the consecutive
terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 … is an arithmetic sequence with
common difference of 2. The next term of this sequence is 15 + 2 = 17. Similarly, 10, 7, 4 … is also an
arithmetic sequence with common difference of -3.
A geometric sequence, on the other hand, is a sequence of numbers such that the ratio between the
consecutive terms is constant. For instance, the sequence 2, 6, 18, 54, 162 … is a geometric sequence
with common ratio of 3. The next term of this sequence is 162 * 3 = 486.
Given a sequence of N integer numbers, your task is to predict the (N+1)th term in the sequence. The
given sequence is guaranteed to be either an arithmetic sequence or a geometric sequence. In case it is
a geometric sequence, the ratio is guaranteed to be an integer number. The (N+1)th term is guaranteed
to fit in an integer variable.
Input
The first line of the input contains the number of test cases, T (1 <= T <= 100). Then T test cases follow.
Each case will consist of one line containing a single integer N (3 <= N <= 1000), followed by a line
containing N space-separated integers, which are the first N terms of the sequence.
Output
For each test case, output one line containing a single integer X, where X is the (N+1)th term of the given
sequence.
Problem C
Heat Streak
Time Limit: 1 second
Professor K has defined Heat Streak as a sequence of maximal successive days where the daily maximum
temperature either increased or remained the same from one day to the next. The total number of days
in such a sequence is called the length of the Heat Streak. You are given the maximum temperature data
of several days. You need to find the length of the Maximum Heat Streak. A Heat Streak is called
Maximum Heat Streak if its length is maximum amongst all the Heat Streaks observed in the given data.
For example, consider the temperature data: 20 18 20 21 23 22 11. Here we find a 1 day heat streak
{20}, followed by a 4 day heat streak {18 20 21 23}, followed by a 1 day heat streak {22}, and then
another 1 day heat streak {11}. Note that the sequence {18 20} is not a heat streak. This is because the
sequence is increasing, but it is not maximal. We can further add 21 and 23 to the sequence to make it
maximal.
Input
First line of the input contains T, the number of test cases (1 <= T <= 20). Each test case is specified in a
single line. The line contains an integer n that represents the number of days for which temperature
data is available (1 <= n <= 104). Then n integers follow, representing maximum temperature of each
successive day. Each temperature will be in the range of 1 to 60. The numbers are separated from each
other by a single space.
Output
For each test case output in a single line the length of the Maximum Heat Streak.
Problem D
Twin Strings
Time Limit: 1 second
Given two strings s1 and s2, determine if they are twins. Two strings are twins if one string can be
transformed to the other using some sort of character mapping. A character mapping is valid if it abides
by the following rules:
A character is mapped to either itself or to exactly one other character.
No two characters map to the same character.
For example:
“book”, “meet” are Twins. The mapping used to transform the former to the latter is: b m, o
e, k t.
“cola”, “cold” are Twins. Replacement mapping is c c, o o, l l, a d.
“book”, “goat” are not Twin. No Valid character mapping exists for the transformation.
Input
The first line of the input gives the number of test cases, T (1 <= T <= 20). Each of following lines will
consist of two space separated strings s1 and s2. Both strings will be of same length and will consist only
of lowercase letters. The maximum length of each string can be 10000.
Output
For each test case output a single line. Print “yes” if the input strings are twins, else print “no”.
Problem E
String Multiplication
Time Limit: 2 second
Consider the operation of multiplying a string by a positive integer. The product of a string s and a
positive integer n is denoted by s*n. This product is obtained by repeating s, n times. For example,
“abc”*3 = ”abcabcabc”.
In this problem, a string s is given. You have to find string p of minimum length such that there exists a
positive integer k for which p*k equals s.
Input
The first line of the input gives the number of test cases, T. Then T test cases follow. Each will consist of
one line containing a single word s, which is the given string.
Limits
1<= T <=20
Output
For each test case, output one line containing a single word p of minimum length such that there exists
a positive integer k for which p*k equals s.
Problem F
Count the Way
Time Limit: 1 second
RAD lives in a 2D grid! He lives in (1, 1) cell of the grid (the bottom and left most cell) and he wants to go
to school which is in (N, M) cell. But he doesn’t know, in how many ways he can go to school from his
home. His mother told him that “RAD, you can move only right and up” and you must not visit the dark
cells. RAD agrees with his mom; but he has to pick his best friend ANI who is in (X, Y) cell. It is
guaranteed that (X, Y) cell is not a dark cell.
So, you have to count the number of ways to go from (1, 1) to (N, M) such that (X, Y) cell is visited and
no dark cell is visited. Only moves allowed are to the right and up directions.
Dark Ani
Cell (X, Y)
Dark
Cell
House
(1, 1)
Input
The first line of the input gives the number of test cases, T (1 <= T <= 20). First line of each test case
contains two space separated positive integers N, M representing the location of the school in the 2D
grid (1 <= N, M <= 50). Next line contains another two space separated positive integers X, Y
representing the position of ANI (1 <= X <= N, 1 <= Y <= M).
Next line contains a non negative integer K representing the number of dark cells (0 <= K <= min(50,
N*M)). Each of next K lines contains two space separated integers DXi, DYi representing the dark cells (1
<= DXi <= N, 1 <= DYi <= M). RAD’s house, school and ANI -- none of them will be in dark cells.
Output
For each test case, output the number of ways RAD can go from house to school maintaining the
requirements. This number can be very large so print the result module 10004.
2 -3 -1
2 2 10 -4
N9W2N3W5S9E2S6
3 5
E9S9E5S2W4N9W3S7
3 17
6 1
5 7 9 11 13 15 486
3
10 7 4
5
2 6 18 54 162
2 1
5 30 29 28 27 26 4
7 20 18 20 21 23 22 11
। yes
no
3 yes
book meet yes
cola cold no
book goat
10 AB
ABAB AB
ABABABAB A
AAA ABC
ABC A
A ASA
ASAASA ASDAS
ASDASASDAS ASDAS
ASDAS ASDA
ASDA AASA
AASAAASA
2 20
5 4 8
4 3
0
5 4
4 3
4
2 4
3 3
3 4
5 2