公式

B - 筋トレの目標達成 / Achieving the Muscle Training Goal 解説 by admin

swe-2-high

Solution

For each member \(i\), scan the string \(S_i\) from left to right and find the first position where o appears. If that position is \(j\) (0-indexed), the answer is day \(j + 1\). If no o appears at all, print -1.

The implementation is straightforward. First read the header line containing \(H\) and \(W\), then read exactly \(H\) row strings. For each row, perform a linear scan (a for loop in C++, str.find in Python) and record the position of the first o.

Proof of Correctness

Claim: For each member \(i\), the value printed by the program equals the earliest day (1-indexed) on which o appears in \(S_i\); if no such day exists, the program prints -1.

Proof: The scan examines the characters of \(S_i\) in the order \(j = 0, 1, \ldots, W - 1\) and stops at the first o found. Hence every position \(j' < j\) satisfies \(S_i[j'] = \) x, while \(S_i[j] = \) o. Therefore \(j\) is the minimum index where o occurs, and since days are 1-indexed the answer is \(j + 1\). If the scan finishes without finding any o, every character of \(S_i\) is x, so the program outputs -1 as required.

Even if o occurs multiple times, only the first occurrence is used, so any tie-breaking (a member achieving the goal on several days) is automatically resolved in favor of the earliest day.

Complexity

  • Time: \(O(HW)\), including reading the input.
  • Space: \(O(W)\) for C++ and \(O(H + W)\) for Python, including stored input and output arrays or strings.