Official

B - 最後の大根 / The Last Radish Editorial by admin Editorial for printing

swe-2-high

To find the rightmost radish, scan the string left to right and overwrite the answer with the current position (\(i+1\), since positions are 1-indexed) every time a D is found. After the loop, ans holds the position of the last D seen — exactly the rightmost radish. If no D ever appears, ans keeps its initial value \(-1\), matching the required output. Each character is inspected once, so the running time is \(O(N)\), and storing the string takes \(O(N)\) space.

posted:
last update: