Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CENG 316

METU Take-Home Exam 2 Practice of Algorithms


Computer Engineering (Deadline: 4th of May 2024, Saturday, 23:59) Spring 2023-2024

1 Problem

After successfully contacting aliens for the first time, humanity established friendly relations with many
alien races. However, the vast number and variety of alien languages remain the biggest obstacle to
freely communicating with them. Your xenolinguist friend asked for your help to analyze an alien
language. After translation into the English alphabet, each word in the language obeys the following
rules:
1. It is a permutation of the first R×C letters of the English alphabet.
2. It does not have any consecutive letters next to each other.
3. When the word is placed, row by row, on an R×C grid (with R rows and C columns), the letters
are in increasing lexicographic (dictionary) order within each row and each column (from left
to right, and from top to bottom).
For R = 4 and C = 3, the following are examples of valid/invalid words:
ACEBFHDGKGJL ACEBDGFIKHJL ACFBDHEJKGIL ACEBDHFIKGJL

A C F A C E
B D H B D H
E J K F I K
G I L G J L

Invalid. Consecutive Invalid. 2nd column


Invalid. Not a per-
letters F and G are is not in ascending Valid.
mutation of A . . L .
adjacent. order.
The words in the language are traditionally ordered using the regular English lexicographic order.
Your friend asks you to create a program that can:
(a) Given a word, determine its rank in lexicographic order (first word being at rank 1), and
(b) Given a lexicographic rank, determine the corresponding word.

2 Input Format

R C
K
Q

R = The value of R (i.e., the number of rows in the grid).


C = The value of C (i.e., the number of columns in the grid).
K = A character describing the query kind. Either w for determining the position of a given word or r
for determining the word corresponding to a given rank.
Q = Query. A string if K is w, an integer if K is r.

3 Output Format

A = Answer to the query. An integer if input K was w, a string if K was r.

Page 1 of 3
4 Limits

2 ≤ R, C.
4 ≤ R×C ≤ 26.
All values of Q are valid.
Time Limit: 0.25 seconds, Memory Limit: 256 MB, Stack Limit: 8 MB

5 Clarifications

• Your solution is expected as a C++ program source named the2.cpp that reads from the stan-
dard input and writes to the standard output.
• It is OK to copy code from the sample codes we shared in our course website in ODTÜClass. You
can also copy code from your previous THE submissions for this course. Copying from elsewhere
will be considered cheating.
• You are supposed to submit your code via the VPL item in ODTÜClass. Use the “evaluate” feature
to run the auto-grader on your submission.
• The grade from the auto-grader is not final. We can later do further evaluations of your code
and adjust your grade. Solutions that do not attempt a “reasonable solution” to the given task
may lose points.
• Your code will be compiled on g++ with the options:
-std=c++2a -O3 -lm -Wall -Wextra -Wpedantic
• Late submissions are not allowed.

6 Sample Input/Output Pairs

Input Output Input Output Input Output


4 4 3 4 4 3
w w w
ACGJBEHMDFKOILNP 644 ACFHBEIKDGJL 8 ADGBFJCHKEIL 56

Input Output Input Output Input Output


6 3 4 2 5 3
r r r
10212 ADHBGLCINEJPFKQMOR 1 ADBFCGEH 867 AEHBFLCIMDJNGKO

Input Output Input Output


5 5 4 5
w r
ADGJNBFIPUCHLRVEKMTXOQSWY 11111111 15000 ACFKOBEILQDGJMSHNPRT

7 Hints

• It is guaranteed that the number of valid words is less than 231 for any R, C pair.
• We suggest that you implement the algorithm that we discussed in our lecture. Recall that the
algorithm
– As an inner step, counts the number of valid words that start with a particular prefix via
dynamic programming. The dynamic programming requires solving O((R + 1)C × C) sub-
problems; each one solved in O(C) time.

Page 2 of 3
– In the outer step, repeatedly fixes the next letter of the word by going through the available
letters in increasing order and performing the above counting queries with the formed
prefix. (In the w case, it computes a partial rank by going through the available letters.)
The outer step requires fixing O(RC) letters each taking O(RC) queries.
Hence, the algorithm takes O(R2 C 2 × (R + 1)C × C 2 ) time. If implemented properly, this al-
gorithm gets full points. In fact, even if you increase the cost by a factor of O(C) (to convert
the subproblems into dynamic programming memoization table indices), you would still get full
points.
• We strongly suggest using a top-down dynamic programming approach. It is likely easier and
faster than traversing the whole memoization table.
• In case you cannot implement aforementioned algorithm, implement the fastest algorithm you
can, to get partial points.

Page 3 of 3

You might also like