B - 宝物の地図 / Treasure Map 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
We check whether each row contains T, which represents a treasure. If it contains T, we assign o; otherwise, we assign x. We then concatenate these characters from the top row to the bottom row and output the resulting string.
Analysis
Whether a row contains a treasure can be determined solely by inspecting the string representing that row. Furthermore, the number and positions of the treasures are not required; only whether there is at least \(1\) T matters.
In Python, you can check whether a string s contains T using 'T' in s.
For example, suppose the rows are as follows:
..T.
....
T..T
- Row \(1\) contains
T, so it corresponds too. - Row \(2\) does not contain
T, so it corresponds tox. - Row \(3\) contains
T, so it corresponds too.
Therefore, the output is oxo.
In this problem, even if we inspect all cells naively, there are at most \(10 \times 10 = 100\) cells, so it will not cause a TLE. No special optimizations are needed. Note, however, that you must output exactly \(1\) character per row, rather than outputting o for each treasure.
Algorithm
- Split the input by whitespace or newlines to obtain the number of rows \(H\) and the string for each row.
- From the top row to the bottom row, evaluate each string
s:- If
'T' in sis true, chooseo. - If false, choose
x.
- If
- Concatenate the \(H\) chosen characters using
joinand print the result.
Since we select a character corresponding to the presence or absence of T in each row and arrange them in the same order as the input, we obtain the desired string.
Complexity
- Time Complexity: \(O(HW)\)
Checking whether each row containsTtakes at most \(O(W)\), and this is performed for \(H\) rows. - Space Complexity: \(O(HW)\)
The provided code reads and stores the entire input. An additional \(O(H)\) space is used for the output string.
Implementation Details
sys.stdin.read().split()splits the input by whitespace and newlines.data[0]is \(H\),data[1]is \(W\), anddata[2]onward are the strings for each row.data[2:2 + H]retrieves the \(H\) rows of the map in input order.Since \(W\) is given as the length of each string, there is no need to store it in a variable for this check.
By using
''.join(...), you can construct a string of length \(H\) without spaces betweenoandx.Source Code
import sys
def main():
data = sys.stdin.read().split()
H = int(data[0])
# W is given but not needed beyond parsing
rows = data[2:2 + H]
print(''.join('o' if 'T' in s else 'x' for s in rows))
main()
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: