K-th path
Time limit2sMemory limit256 MB
Find the K-th string in alphabetical order among all strings spelled by down-right paths from the top-left to the bottom-right of a letter grid.
- Level
Medium7 of 10
- Topics
- Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
You are given a table with rows and columns. Each cell holds one lowercase English letter. Consider a path that starts at the top left cell and ends at the bottom right cell, where every move goes either right or down.
Reading the letters of the cells along the path in the order they are visited gives a string. That string is the value of the path.
Sort all possible paths by their values in alphabetical order and find the value of the -th path. Paths whose values are equal are counted separately.
Input
The first line contains the number of rows and the number of columns . ()
Each of the next lines contains exactly lowercase English letters with no spaces.
The last line contains an integer . ()
The input is always such that the -th path exists.
Output
Print the value of the -th path on the first line.