Official

A - 連続勝利の記録 / Record of Consecutive Wins Editorial by admin

gpt-5.3-codex

Overview

By scanning the string \(S\) from left to right exactly once and continuously counting how many consecutive Ws there are, we can find the longest winning streak. When an L appears, the streak is broken, so the key point is to reset the count to \(0\).

Analysis

This problem asks us to find the “maximum length of a consecutive W segment.” The important observation is that at each position, all we need is “the current streak length” and “the maximum value so far.”

For example, consider S = "WWLWWWLW":

  • W → streak \(1\) (max \(1\))
  • W → streak \(2\) (max \(2\))
  • L → reset streak to \(0\)
  • W → streak \(1\)
  • W → streak \(2\)
  • W → streak \(3\) (max \(3\))
  • L → reset streak to \(0\)
  • W → streak \(1\)

The final maximum is \(3\).


A naive approach of “checking all possible intervals” would be too inefficient due to the large number of intervals (worst case \(O(N^2)\) or more). Since the constraint is \(N \le 10^6\), we need to solve it with a single pass through each character (\(O(N)\)).

The following rules are sufficient for this problem:

  • If the character is W, then current += 1
  • If the character is L, then current = 0
  • Each time, update max_streak = max(max_streak, current)

This way, even when there are no Ws at all, max_streak remains at its initial value of \(0\), giving the correct answer.

Algorithm

  1. max_streak = 0 (maximum consecutive wins so far)
  2. current = 0 (current consecutive wins)
  3. Scan the string \(S\) from left to right:
    • If the character is W, increment current by 1
    • If the character is L, set current to 0
    • Update max_streak with max(max_streak, current)
  4. Output max_streak at the end

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\) (excluding the input string)

Implementation Notes

  • Don’t forget to set current = 0 when encountering an L.

  • Updating max_streak only when the character is W also works correctly (the provided code uses this form).

  • Since the input can be large, using sys.stdin.readline in Python ensures stable and fast performance.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    n = int(input().strip())
    s = input().strip()

    max_streak = 0
    current = 0

    for ch in s:
        if ch == 'W':
            current += 1
            if current > max_streak:
                max_streak = current
        else:
            current = 0

    print(max_streak)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.3-codex.

posted:
last update: