B - 手記の縦読み / Vertical Reading of a Memoir Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
This problem asks you to concatenate the \(K\)-th character from the left of each row, from top to bottom, and output the result.
Analysis
Only the character in the \(K\)-th column of each row is used for the secret message. Therefore, you can obtain the answer simply by examining the strings from top to bottom and extracting the required characters.
For example, consider the case where \(H=3\), \(W=4\), and \(K=2\), and the manuscript is as follows:
abcd
efgh
ijkl
The \(2\)-nd character from the left of each row is b, f, and j, so the answer is bfj.
In this problem, the straightforward approach of extracting characters row by row is fast enough. No special optimizations are needed.
However, note that while the problem statement uses 1-based indexing for columns, Python uses 0-based indexing for strings. If you use row[k], it will access the next column or result in an out-of-range index error, so make sure to use row[k - 1].
Algorithm
- Read \(H, W, K\).
- Store the \(H\) strings in a list
rowsfrom top to bottom. - Extract
row[k - 1]from each stringrow. - Concatenate the extracted characters in order and output the resulting string.
Since rows stores the lines in the order they were input, processing them sequentially produces the message read from top to bottom.
Complexity
- Time complexity: \(O(HW)\)
- Space complexity: \(O(HW)\)
This is because we read and store a total of \(HW\) input characters in rows. After reading the input, the time complexity for constructing the message alone is \(O(H)\).
Implementation Notes
In Python, the \(K\)-th character from the left can be retrieved using
row[k - 1].Using
"".join(...)allows you to concatenate the extracted characters without delimiters.Output the answer on a single line as a string of length \(H\).
Source Code
# 問題文から独立に実装
h, w, k = map(int, input().split())
rows = [input().strip() for _ in range(h)]
print("".join(row[k - 1] for row in rows))
This editorial was generated by gpt-6-astra-medium.
posted:
last update: